sb_edac.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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
  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.1.1 "
  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) & GENMASK_ULL(hi, lo)) >> (lo))
  47. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  48. static const u32 sbridge_dram_rule[] = {
  49. 0x80, 0x88, 0x90, 0x98, 0xa0,
  50. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  51. };
  52. static const u32 ibridge_dram_rule[] = {
  53. 0x60, 0x68, 0x70, 0x78, 0x80,
  54. 0x88, 0x90, 0x98, 0xa0, 0xa8,
  55. 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
  56. 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
  57. };
  58. #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
  59. #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
  60. #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
  61. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  62. #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
  63. static char *get_dram_attr(u32 reg)
  64. {
  65. switch(DRAM_ATTR(reg)) {
  66. case 0:
  67. return "DRAM";
  68. case 1:
  69. return "MMCFG";
  70. case 2:
  71. return "NXM";
  72. default:
  73. return "unknown";
  74. }
  75. }
  76. static const u32 sbridge_interleave_list[] = {
  77. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  78. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  79. };
  80. static const u32 ibridge_interleave_list[] = {
  81. 0x64, 0x6c, 0x74, 0x7c, 0x84,
  82. 0x8c, 0x94, 0x9c, 0xa4, 0xac,
  83. 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
  84. 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
  85. };
  86. struct interleave_pkg {
  87. unsigned char start;
  88. unsigned char end;
  89. };
  90. static const struct interleave_pkg sbridge_interleave_pkg[] = {
  91. { 0, 2 },
  92. { 3, 5 },
  93. { 8, 10 },
  94. { 11, 13 },
  95. { 16, 18 },
  96. { 19, 21 },
  97. { 24, 26 },
  98. { 27, 29 },
  99. };
  100. static const struct interleave_pkg ibridge_interleave_pkg[] = {
  101. { 0, 3 },
  102. { 4, 7 },
  103. { 8, 11 },
  104. { 12, 15 },
  105. { 16, 19 },
  106. { 20, 23 },
  107. { 24, 27 },
  108. { 28, 31 },
  109. };
  110. static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
  111. int interleave)
  112. {
  113. return GET_BITFIELD(reg, table[interleave].start,
  114. table[interleave].end);
  115. }
  116. /* Devices 12 Function 7 */
  117. #define TOLM 0x80
  118. #define TOHM 0x84
  119. #define HASWELL_TOLM 0xd0
  120. #define HASWELL_TOHM_0 0xd4
  121. #define HASWELL_TOHM_1 0xd8
  122. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  123. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  124. /* Device 13 Function 6 */
  125. #define SAD_TARGET 0xf0
  126. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  127. #define SAD_CONTROL 0xf4
  128. /* Device 14 function 0 */
  129. static const u32 tad_dram_rule[] = {
  130. 0x40, 0x44, 0x48, 0x4c,
  131. 0x50, 0x54, 0x58, 0x5c,
  132. 0x60, 0x64, 0x68, 0x6c,
  133. };
  134. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  135. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  136. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  137. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  138. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  139. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  140. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  141. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  142. /* Device 15, function 0 */
  143. #define MCMTR 0x7c
  144. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  145. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  146. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  147. /* Device 15, function 1 */
  148. #define RASENABLES 0xac
  149. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  150. /* Device 15, functions 2-5 */
  151. static const int mtr_regs[] = {
  152. 0x80, 0x84, 0x88,
  153. };
  154. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  155. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  156. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  157. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  158. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  159. static const u32 tad_ch_nilv_offset[] = {
  160. 0x90, 0x94, 0x98, 0x9c,
  161. 0xa0, 0xa4, 0xa8, 0xac,
  162. 0xb0, 0xb4, 0xb8, 0xbc,
  163. };
  164. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  165. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  166. static const u32 rir_way_limit[] = {
  167. 0x108, 0x10c, 0x110, 0x114, 0x118,
  168. };
  169. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  170. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  171. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  172. #define MAX_RIR_WAY 8
  173. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  174. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  175. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  176. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  177. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  178. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  179. };
  180. #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
  181. #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
  182. /* Device 16, functions 2-7 */
  183. /*
  184. * FIXME: Implement the error count reads directly
  185. */
  186. static const u32 correrrcnt[] = {
  187. 0x104, 0x108, 0x10c, 0x110,
  188. };
  189. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  190. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  191. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  192. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  193. static const u32 correrrthrsld[] = {
  194. 0x11c, 0x120, 0x124, 0x128,
  195. };
  196. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  197. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  198. /* Device 17, function 0 */
  199. #define SB_RANK_CFG_A 0x0328
  200. #define IB_RANK_CFG_A 0x0320
  201. /*
  202. * sbridge structs
  203. */
  204. #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
  205. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  206. #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
  207. enum type {
  208. SANDY_BRIDGE,
  209. IVY_BRIDGE,
  210. HASWELL,
  211. BROADWELL,
  212. };
  213. struct sbridge_pvt;
  214. struct sbridge_info {
  215. enum type type;
  216. u32 mcmtr;
  217. u32 rankcfgr;
  218. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  219. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  220. u64 (*rir_limit)(u32 reg);
  221. const u32 *dram_rule;
  222. const u32 *interleave_list;
  223. const struct interleave_pkg *interleave_pkg;
  224. u8 max_sad;
  225. u8 max_interleave;
  226. u8 (*get_node_id)(struct sbridge_pvt *pvt);
  227. enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
  228. enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
  229. struct pci_dev *pci_vtd;
  230. };
  231. struct sbridge_channel {
  232. u32 ranks;
  233. u32 dimms;
  234. };
  235. struct pci_id_descr {
  236. int dev_id;
  237. int optional;
  238. };
  239. struct pci_id_table {
  240. const struct pci_id_descr *descr;
  241. int n_devs;
  242. };
  243. struct sbridge_dev {
  244. struct list_head list;
  245. u8 bus, mc;
  246. u8 node_id, source_id;
  247. struct pci_dev **pdev;
  248. int n_devs;
  249. struct mem_ctl_info *mci;
  250. };
  251. struct sbridge_pvt {
  252. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  253. struct pci_dev *pci_sad0, *pci_sad1;
  254. struct pci_dev *pci_ha0, *pci_ha1;
  255. struct pci_dev *pci_br0, *pci_br1;
  256. struct pci_dev *pci_ha1_ta;
  257. struct pci_dev *pci_tad[NUM_CHANNELS];
  258. struct sbridge_dev *sbridge_dev;
  259. struct sbridge_info info;
  260. struct sbridge_channel channel[NUM_CHANNELS];
  261. /* Memory type detection */
  262. bool is_mirrored, is_lockstep, is_close_pg;
  263. /* Fifo double buffers */
  264. struct mce mce_entry[MCE_LOG_LEN];
  265. struct mce mce_outentry[MCE_LOG_LEN];
  266. /* Fifo in/out counters */
  267. unsigned mce_in, mce_out;
  268. /* Count indicator to show errors not got */
  269. unsigned mce_overrun;
  270. /* Memory description */
  271. u64 tolm, tohm;
  272. };
  273. #define PCI_DESCR(device_id, opt) \
  274. .dev_id = (device_id), \
  275. .optional = opt
  276. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  277. /* Processor Home Agent */
  278. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  279. /* Memory controller */
  280. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  281. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  282. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  283. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  284. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  285. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  286. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  287. /* System Address Decoder */
  288. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  289. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  290. /* Broadcast Registers */
  291. { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  292. };
  293. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  294. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  295. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
  296. {0,} /* 0 terminated list. */
  297. };
  298. /* This changes depending if 1HA or 2HA:
  299. * 1HA:
  300. * 0x0eb8 (17.0) is DDRIO0
  301. * 2HA:
  302. * 0x0ebc (17.4) is DDRIO0
  303. */
  304. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
  305. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
  306. /* pci ids */
  307. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
  308. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
  309. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
  310. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
  311. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
  312. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
  313. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
  314. #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
  315. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
  316. #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
  317. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
  318. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
  319. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
  320. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
  321. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
  322. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
  323. #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
  324. static const struct pci_id_descr pci_dev_descr_ibridge[] = {
  325. /* Processor Home Agent */
  326. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
  327. /* Memory controller */
  328. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
  329. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
  330. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
  331. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
  332. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
  333. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
  334. /* System Address Decoder */
  335. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
  336. /* Broadcast Registers */
  337. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
  338. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
  339. /* Optional, mode 2HA */
  340. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
  341. #if 0
  342. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
  343. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
  344. #endif
  345. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
  346. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
  347. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
  348. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
  349. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
  350. { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
  351. };
  352. static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
  353. PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
  354. {0,} /* 0 terminated list. */
  355. };
  356. /* Haswell support */
  357. /* EN processor:
  358. * - 1 IMC
  359. * - 3 DDR3 channels, 2 DPC per channel
  360. * EP processor:
  361. * - 1 or 2 IMC
  362. * - 4 DDR4 channels, 3 DPC per channel
  363. * EP 4S processor:
  364. * - 2 IMC
  365. * - 4 DDR4 channels, 3 DPC per channel
  366. * EX processor:
  367. * - 2 IMC
  368. * - each IMC interfaces with a SMI 2 channel
  369. * - each SMI channel interfaces with a scalable memory buffer
  370. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  371. */
  372. #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
  373. #define HASWELL_HASYSDEFEATURE2 0x84
  374. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
  375. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
  376. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
  377. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
  378. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
  379. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
  380. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
  381. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
  382. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
  383. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
  384. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
  385. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
  386. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
  387. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
  388. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
  389. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
  390. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
  391. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
  392. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
  393. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
  394. #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
  395. static const struct pci_id_descr pci_dev_descr_haswell[] = {
  396. /* first item must be the HA */
  397. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
  398. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
  399. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
  400. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
  401. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
  402. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
  403. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
  404. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
  405. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
  406. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
  407. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
  408. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
  409. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
  410. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
  411. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
  412. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
  413. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
  414. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
  415. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
  416. { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
  417. };
  418. static const struct pci_id_table pci_dev_descr_haswell_table[] = {
  419. PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
  420. {0,} /* 0 terminated list. */
  421. };
  422. /*
  423. * Broadwell support
  424. *
  425. * DE processor:
  426. * - 1 IMC
  427. * - 2 DDR3 channels, 2 DPC per channel
  428. * EP processor:
  429. * - 1 or 2 IMC
  430. * - 4 DDR4 channels, 3 DPC per channel
  431. * EP 4S processor:
  432. * - 2 IMC
  433. * - 4 DDR4 channels, 3 DPC per channel
  434. * EX processor:
  435. * - 2 IMC
  436. * - each IMC interfaces with a SMI 2 channel
  437. * - each SMI channel interfaces with a scalable memory buffer
  438. * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
  439. */
  440. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
  441. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
  442. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
  443. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
  444. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
  445. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
  446. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
  447. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
  448. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
  449. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
  450. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
  451. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
  452. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
  453. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
  454. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
  455. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
  456. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
  457. #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
  458. static const struct pci_id_descr pci_dev_descr_broadwell[] = {
  459. /* first item must be the HA */
  460. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
  461. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
  462. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
  463. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
  464. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
  465. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
  466. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
  467. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
  468. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
  469. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
  470. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
  471. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
  472. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
  473. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
  474. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
  475. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
  476. { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
  477. };
  478. static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
  479. PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
  480. {0,} /* 0 terminated list. */
  481. };
  482. /*
  483. * pci_device_id table for which devices we are looking for
  484. */
  485. static const struct pci_device_id sbridge_pci_tbl[] = {
  486. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
  487. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
  488. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
  489. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0)},
  490. {0,} /* 0 terminated list. */
  491. };
  492. /****************************************************************************
  493. Ancillary status routines
  494. ****************************************************************************/
  495. static inline int numrank(enum type type, u32 mtr)
  496. {
  497. int ranks = (1 << RANK_CNT_BITS(mtr));
  498. int max = 4;
  499. if (type == HASWELL || type == BROADWELL)
  500. max = 8;
  501. if (ranks > max) {
  502. edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
  503. ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  504. return -EINVAL;
  505. }
  506. return ranks;
  507. }
  508. static inline int numrow(u32 mtr)
  509. {
  510. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  511. if (rows < 13 || rows > 18) {
  512. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  513. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  514. return -EINVAL;
  515. }
  516. return 1 << rows;
  517. }
  518. static inline int numcol(u32 mtr)
  519. {
  520. int cols = (COL_WIDTH_BITS(mtr) + 10);
  521. if (cols > 12) {
  522. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  523. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  524. return -EINVAL;
  525. }
  526. return 1 << cols;
  527. }
  528. static struct sbridge_dev *get_sbridge_dev(u8 bus)
  529. {
  530. struct sbridge_dev *sbridge_dev;
  531. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  532. if (sbridge_dev->bus == bus)
  533. return sbridge_dev;
  534. }
  535. return NULL;
  536. }
  537. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  538. const struct pci_id_table *table)
  539. {
  540. struct sbridge_dev *sbridge_dev;
  541. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  542. if (!sbridge_dev)
  543. return NULL;
  544. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  545. GFP_KERNEL);
  546. if (!sbridge_dev->pdev) {
  547. kfree(sbridge_dev);
  548. return NULL;
  549. }
  550. sbridge_dev->bus = bus;
  551. sbridge_dev->n_devs = table->n_devs;
  552. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  553. return sbridge_dev;
  554. }
  555. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  556. {
  557. list_del(&sbridge_dev->list);
  558. kfree(sbridge_dev->pdev);
  559. kfree(sbridge_dev);
  560. }
  561. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  562. {
  563. u32 reg;
  564. /* Address range is 32:28 */
  565. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  566. return GET_TOLM(reg);
  567. }
  568. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  569. {
  570. u32 reg;
  571. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  572. return GET_TOHM(reg);
  573. }
  574. static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
  575. {
  576. u32 reg;
  577. pci_read_config_dword(pvt->pci_br1, TOLM, &reg);
  578. return GET_TOLM(reg);
  579. }
  580. static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
  581. {
  582. u32 reg;
  583. pci_read_config_dword(pvt->pci_br1, TOHM, &reg);
  584. return GET_TOHM(reg);
  585. }
  586. static u64 rir_limit(u32 reg)
  587. {
  588. return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
  589. }
  590. static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
  591. {
  592. u32 reg;
  593. enum mem_type mtype;
  594. if (pvt->pci_ddrio) {
  595. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  596. &reg);
  597. if (GET_BITFIELD(reg, 11, 11))
  598. /* FIXME: Can also be LRDIMM */
  599. mtype = MEM_RDDR3;
  600. else
  601. mtype = MEM_DDR3;
  602. } else
  603. mtype = MEM_UNKNOWN;
  604. return mtype;
  605. }
  606. static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
  607. {
  608. u32 reg;
  609. bool registered = false;
  610. enum mem_type mtype = MEM_UNKNOWN;
  611. if (!pvt->pci_ddrio)
  612. goto out;
  613. pci_read_config_dword(pvt->pci_ddrio,
  614. HASWELL_DDRCRCLKCONTROLS, &reg);
  615. /* Is_Rdimm */
  616. if (GET_BITFIELD(reg, 16, 16))
  617. registered = true;
  618. pci_read_config_dword(pvt->pci_ta, MCMTR, &reg);
  619. if (GET_BITFIELD(reg, 14, 14)) {
  620. if (registered)
  621. mtype = MEM_RDDR4;
  622. else
  623. mtype = MEM_DDR4;
  624. } else {
  625. if (registered)
  626. mtype = MEM_RDDR3;
  627. else
  628. mtype = MEM_DDR3;
  629. }
  630. out:
  631. return mtype;
  632. }
  633. static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  634. {
  635. /* there's no way to figure out */
  636. return DEV_UNKNOWN;
  637. }
  638. static enum dev_type __ibridge_get_width(u32 mtr)
  639. {
  640. enum dev_type type;
  641. switch (mtr) {
  642. case 3:
  643. type = DEV_UNKNOWN;
  644. break;
  645. case 2:
  646. type = DEV_X16;
  647. break;
  648. case 1:
  649. type = DEV_X8;
  650. break;
  651. case 0:
  652. type = DEV_X4;
  653. break;
  654. }
  655. return type;
  656. }
  657. static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
  658. {
  659. /*
  660. * ddr3_width on the documentation but also valid for DDR4 on
  661. * Haswell
  662. */
  663. return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
  664. }
  665. static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
  666. {
  667. /* ddr3_width on the documentation but also valid for DDR4 */
  668. return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
  669. }
  670. static u8 get_node_id(struct sbridge_pvt *pvt)
  671. {
  672. u32 reg;
  673. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  674. return GET_BITFIELD(reg, 0, 2);
  675. }
  676. static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
  677. {
  678. u32 reg;
  679. pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, &reg);
  680. return GET_BITFIELD(reg, 0, 3);
  681. }
  682. static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
  683. {
  684. u32 reg;
  685. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, &reg);
  686. return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
  687. }
  688. static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
  689. {
  690. u64 rc;
  691. u32 reg;
  692. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, &reg);
  693. rc = GET_BITFIELD(reg, 26, 31);
  694. pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, &reg);
  695. rc = ((reg << 6) | rc) << 26;
  696. return rc | 0x1ffffff;
  697. }
  698. static u64 haswell_rir_limit(u32 reg)
  699. {
  700. return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
  701. }
  702. static inline u8 sad_pkg_socket(u8 pkg)
  703. {
  704. /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
  705. return ((pkg >> 3) << 2) | (pkg & 0x3);
  706. }
  707. static inline u8 sad_pkg_ha(u8 pkg)
  708. {
  709. return (pkg >> 2) & 0x1;
  710. }
  711. /****************************************************************************
  712. Memory check routines
  713. ****************************************************************************/
  714. static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
  715. {
  716. struct pci_dev *pdev = NULL;
  717. do {
  718. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
  719. if (pdev && pdev->bus->number == bus)
  720. break;
  721. } while (pdev);
  722. return pdev;
  723. }
  724. /**
  725. * check_if_ecc_is_active() - Checks if ECC is active
  726. * @bus: Device bus
  727. * @type: Memory controller type
  728. * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
  729. * disabled
  730. */
  731. static int check_if_ecc_is_active(const u8 bus, enum type type)
  732. {
  733. struct pci_dev *pdev = NULL;
  734. u32 mcmtr, id;
  735. switch (type) {
  736. case IVY_BRIDGE:
  737. id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
  738. break;
  739. case HASWELL:
  740. id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
  741. break;
  742. case SANDY_BRIDGE:
  743. id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
  744. break;
  745. case BROADWELL:
  746. id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
  747. break;
  748. default:
  749. return -ENODEV;
  750. }
  751. pdev = get_pdev_same_bus(bus, id);
  752. if (!pdev) {
  753. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  754. "%04x:%04x! on bus %02d\n",
  755. PCI_VENDOR_ID_INTEL, id, bus);
  756. return -ENODEV;
  757. }
  758. pci_read_config_dword(pdev, MCMTR, &mcmtr);
  759. if (!IS_ECC_ENABLED(mcmtr)) {
  760. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  761. return -ENODEV;
  762. }
  763. return 0;
  764. }
  765. static int get_dimm_config(struct mem_ctl_info *mci)
  766. {
  767. struct sbridge_pvt *pvt = mci->pvt_info;
  768. struct dimm_info *dimm;
  769. unsigned i, j, banks, ranks, rows, cols, npages;
  770. u64 size;
  771. u32 reg;
  772. enum edac_type mode;
  773. enum mem_type mtype;
  774. if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL)
  775. pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, &reg);
  776. else
  777. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  778. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  779. pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
  780. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  781. pvt->sbridge_dev->mc,
  782. pvt->sbridge_dev->node_id,
  783. pvt->sbridge_dev->source_id);
  784. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  785. if (IS_MIRROR_ENABLED(reg)) {
  786. edac_dbg(0, "Memory mirror is enabled\n");
  787. pvt->is_mirrored = true;
  788. } else {
  789. edac_dbg(0, "Memory mirror is disabled\n");
  790. pvt->is_mirrored = false;
  791. }
  792. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  793. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  794. edac_dbg(0, "Lockstep is enabled\n");
  795. mode = EDAC_S8ECD8ED;
  796. pvt->is_lockstep = true;
  797. } else {
  798. edac_dbg(0, "Lockstep is disabled\n");
  799. mode = EDAC_S4ECD4ED;
  800. pvt->is_lockstep = false;
  801. }
  802. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  803. edac_dbg(0, "address map is on closed page mode\n");
  804. pvt->is_close_pg = true;
  805. } else {
  806. edac_dbg(0, "address map is on open page mode\n");
  807. pvt->is_close_pg = false;
  808. }
  809. mtype = pvt->info.get_memory_type(pvt);
  810. if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
  811. edac_dbg(0, "Memory is registered\n");
  812. else if (mtype == MEM_UNKNOWN)
  813. edac_dbg(0, "Cannot determine memory type\n");
  814. else
  815. edac_dbg(0, "Memory is unregistered\n");
  816. if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
  817. banks = 16;
  818. else
  819. banks = 8;
  820. for (i = 0; i < NUM_CHANNELS; i++) {
  821. u32 mtr;
  822. if (!pvt->pci_tad[i])
  823. continue;
  824. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  825. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  826. i, j, 0);
  827. pci_read_config_dword(pvt->pci_tad[i],
  828. mtr_regs[j], &mtr);
  829. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  830. if (IS_DIMM_PRESENT(mtr)) {
  831. pvt->channel[i].dimms++;
  832. ranks = numrank(pvt->info.type, mtr);
  833. rows = numrow(mtr);
  834. cols = numcol(mtr);
  835. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  836. npages = MiB_TO_PAGES(size);
  837. edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  838. pvt->sbridge_dev->mc, i/4, i%4, j,
  839. size, npages,
  840. banks, ranks, rows, cols);
  841. dimm->nr_pages = npages;
  842. dimm->grain = 32;
  843. dimm->dtype = pvt->info.get_width(pvt, mtr);
  844. dimm->mtype = mtype;
  845. dimm->edac_mode = mode;
  846. snprintf(dimm->label, sizeof(dimm->label),
  847. "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
  848. pvt->sbridge_dev->source_id, i/4, i%4, j);
  849. }
  850. }
  851. }
  852. return 0;
  853. }
  854. static void get_memory_layout(const struct mem_ctl_info *mci)
  855. {
  856. struct sbridge_pvt *pvt = mci->pvt_info;
  857. int i, j, k, n_sads, n_tads, sad_interl;
  858. u32 reg;
  859. u64 limit, prv = 0;
  860. u64 tmp_mb;
  861. u32 gb, mb;
  862. u32 rir_way;
  863. /*
  864. * Step 1) Get TOLM/TOHM ranges
  865. */
  866. pvt->tolm = pvt->info.get_tolm(pvt);
  867. tmp_mb = (1 + pvt->tolm) >> 20;
  868. gb = div_u64_rem(tmp_mb, 1024, &mb);
  869. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
  870. gb, (mb*1000)/1024, (u64)pvt->tolm);
  871. /* Address range is already 45:25 */
  872. pvt->tohm = pvt->info.get_tohm(pvt);
  873. tmp_mb = (1 + pvt->tohm) >> 20;
  874. gb = div_u64_rem(tmp_mb, 1024, &mb);
  875. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
  876. gb, (mb*1000)/1024, (u64)pvt->tohm);
  877. /*
  878. * Step 2) Get SAD range and SAD Interleave list
  879. * TAD registers contain the interleave wayness. However, it
  880. * seems simpler to just discover it indirectly, with the
  881. * algorithm bellow.
  882. */
  883. prv = 0;
  884. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  885. /* SAD_LIMIT Address range is 45:26 */
  886. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  887. &reg);
  888. limit = SAD_LIMIT(reg);
  889. if (!DRAM_RULE_ENABLE(reg))
  890. continue;
  891. if (limit <= prv)
  892. break;
  893. tmp_mb = (limit + 1) >> 20;
  894. gb = div_u64_rem(tmp_mb, 1024, &mb);
  895. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  896. n_sads,
  897. get_dram_attr(reg),
  898. gb, (mb*1000)/1024,
  899. ((u64)tmp_mb) << 20L,
  900. INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
  901. reg);
  902. prv = limit;
  903. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  904. &reg);
  905. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  906. for (j = 0; j < 8; j++) {
  907. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
  908. if (j > 0 && sad_interl == pkg)
  909. break;
  910. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  911. n_sads, j, pkg);
  912. }
  913. }
  914. /*
  915. * Step 3) Get TAD range
  916. */
  917. prv = 0;
  918. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  919. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  920. &reg);
  921. limit = TAD_LIMIT(reg);
  922. if (limit <= prv)
  923. break;
  924. tmp_mb = (limit + 1) >> 20;
  925. gb = div_u64_rem(tmp_mb, 1024, &mb);
  926. 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",
  927. n_tads, gb, (mb*1000)/1024,
  928. ((u64)tmp_mb) << 20L,
  929. (u32)TAD_SOCK(reg),
  930. (u32)TAD_CH(reg),
  931. (u32)TAD_TGT0(reg),
  932. (u32)TAD_TGT1(reg),
  933. (u32)TAD_TGT2(reg),
  934. (u32)TAD_TGT3(reg),
  935. reg);
  936. prv = limit;
  937. }
  938. /*
  939. * Step 4) Get TAD offsets, per each channel
  940. */
  941. for (i = 0; i < NUM_CHANNELS; i++) {
  942. if (!pvt->channel[i].dimms)
  943. continue;
  944. for (j = 0; j < n_tads; j++) {
  945. pci_read_config_dword(pvt->pci_tad[i],
  946. tad_ch_nilv_offset[j],
  947. &reg);
  948. tmp_mb = TAD_OFFSET(reg) >> 20;
  949. gb = div_u64_rem(tmp_mb, 1024, &mb);
  950. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  951. i, j,
  952. gb, (mb*1000)/1024,
  953. ((u64)tmp_mb) << 20L,
  954. reg);
  955. }
  956. }
  957. /*
  958. * Step 6) Get RIR Wayness/Limit, per each channel
  959. */
  960. for (i = 0; i < NUM_CHANNELS; i++) {
  961. if (!pvt->channel[i].dimms)
  962. continue;
  963. for (j = 0; j < MAX_RIR_RANGES; j++) {
  964. pci_read_config_dword(pvt->pci_tad[i],
  965. rir_way_limit[j],
  966. &reg);
  967. if (!IS_RIR_VALID(reg))
  968. continue;
  969. tmp_mb = pvt->info.rir_limit(reg) >> 20;
  970. rir_way = 1 << RIR_WAY(reg);
  971. gb = div_u64_rem(tmp_mb, 1024, &mb);
  972. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  973. i, j,
  974. gb, (mb*1000)/1024,
  975. ((u64)tmp_mb) << 20L,
  976. rir_way,
  977. reg);
  978. for (k = 0; k < rir_way; k++) {
  979. pci_read_config_dword(pvt->pci_tad[i],
  980. rir_offset[j][k],
  981. &reg);
  982. tmp_mb = RIR_OFFSET(reg) << 6;
  983. gb = div_u64_rem(tmp_mb, 1024, &mb);
  984. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  985. i, j, k,
  986. gb, (mb*1000)/1024,
  987. ((u64)tmp_mb) << 20L,
  988. (u32)RIR_RNK_TGT(reg),
  989. reg);
  990. }
  991. }
  992. }
  993. }
  994. static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  995. {
  996. struct sbridge_dev *sbridge_dev;
  997. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  998. if (sbridge_dev->node_id == node_id)
  999. return sbridge_dev->mci;
  1000. }
  1001. return NULL;
  1002. }
  1003. static int get_memory_error_data(struct mem_ctl_info *mci,
  1004. u64 addr,
  1005. u8 *socket, u8 *ha,
  1006. long *channel_mask,
  1007. u8 *rank,
  1008. char **area_type, char *msg)
  1009. {
  1010. struct mem_ctl_info *new_mci;
  1011. struct sbridge_pvt *pvt = mci->pvt_info;
  1012. struct pci_dev *pci_ha;
  1013. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  1014. int sad_interl, idx, base_ch;
  1015. int interleave_mode, shiftup = 0;
  1016. unsigned sad_interleave[pvt->info.max_interleave];
  1017. u32 reg, dram_rule;
  1018. u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
  1019. u32 tad_offset;
  1020. u32 rir_way;
  1021. u32 mb, gb;
  1022. u64 ch_addr, offset, limit = 0, prv = 0;
  1023. /*
  1024. * Step 0) Check if the address is at special memory ranges
  1025. * The check bellow is probably enough to fill all cases where
  1026. * the error is not inside a memory, except for the legacy
  1027. * range (e. g. VGA addresses). It is unlikely, however, that the
  1028. * memory controller would generate an error on that range.
  1029. */
  1030. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  1031. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  1032. return -EINVAL;
  1033. }
  1034. if (addr >= (u64)pvt->tohm) {
  1035. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  1036. return -EINVAL;
  1037. }
  1038. /*
  1039. * Step 1) Get socket
  1040. */
  1041. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  1042. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  1043. &reg);
  1044. if (!DRAM_RULE_ENABLE(reg))
  1045. continue;
  1046. limit = SAD_LIMIT(reg);
  1047. if (limit <= prv) {
  1048. sprintf(msg, "Can't discover the memory socket");
  1049. return -EINVAL;
  1050. }
  1051. if (addr <= limit)
  1052. break;
  1053. prv = limit;
  1054. }
  1055. if (n_sads == pvt->info.max_sad) {
  1056. sprintf(msg, "Can't discover the memory socket");
  1057. return -EINVAL;
  1058. }
  1059. dram_rule = reg;
  1060. *area_type = get_dram_attr(dram_rule);
  1061. interleave_mode = INTERLEAVE_MODE(dram_rule);
  1062. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  1063. &reg);
  1064. if (pvt->info.type == SANDY_BRIDGE) {
  1065. sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
  1066. for (sad_way = 0; sad_way < 8; sad_way++) {
  1067. u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
  1068. if (sad_way > 0 && sad_interl == pkg)
  1069. break;
  1070. sad_interleave[sad_way] = pkg;
  1071. edac_dbg(0, "SAD interleave #%d: %d\n",
  1072. sad_way, sad_interleave[sad_way]);
  1073. }
  1074. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  1075. pvt->sbridge_dev->mc,
  1076. n_sads,
  1077. addr,
  1078. limit,
  1079. sad_way + 7,
  1080. !interleave_mode ? "" : "XOR[18:16]");
  1081. if (interleave_mode)
  1082. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  1083. else
  1084. idx = (addr >> 6) & 7;
  1085. switch (sad_way) {
  1086. case 1:
  1087. idx = 0;
  1088. break;
  1089. case 2:
  1090. idx = idx & 1;
  1091. break;
  1092. case 4:
  1093. idx = idx & 3;
  1094. break;
  1095. case 8:
  1096. break;
  1097. default:
  1098. sprintf(msg, "Can't discover socket interleave");
  1099. return -EINVAL;
  1100. }
  1101. *socket = sad_interleave[idx];
  1102. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  1103. idx, sad_way, *socket);
  1104. } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
  1105. int bits, a7mode = A7MODE(dram_rule);
  1106. if (a7mode) {
  1107. /* A7 mode swaps P9 with P6 */
  1108. bits = GET_BITFIELD(addr, 7, 8) << 1;
  1109. bits |= GET_BITFIELD(addr, 9, 9);
  1110. } else
  1111. bits = GET_BITFIELD(addr, 6, 8);
  1112. if (interleave_mode == 0) {
  1113. /* interleave mode will XOR {8,7,6} with {18,17,16} */
  1114. idx = GET_BITFIELD(addr, 16, 18);
  1115. idx ^= bits;
  1116. } else
  1117. idx = bits;
  1118. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1119. *socket = sad_pkg_socket(pkg);
  1120. sad_ha = sad_pkg_ha(pkg);
  1121. if (sad_ha)
  1122. ch_add = 4;
  1123. if (a7mode) {
  1124. /* MCChanShiftUpEnable */
  1125. pci_read_config_dword(pvt->pci_ha0,
  1126. HASWELL_HASYSDEFEATURE2, &reg);
  1127. shiftup = GET_BITFIELD(reg, 22, 22);
  1128. }
  1129. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
  1130. idx, *socket, sad_ha, shiftup);
  1131. } else {
  1132. /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
  1133. idx = (addr >> 6) & 7;
  1134. pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
  1135. *socket = sad_pkg_socket(pkg);
  1136. sad_ha = sad_pkg_ha(pkg);
  1137. if (sad_ha)
  1138. ch_add = 4;
  1139. edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
  1140. idx, *socket, sad_ha);
  1141. }
  1142. *ha = sad_ha;
  1143. /*
  1144. * Move to the proper node structure, in order to access the
  1145. * right PCI registers
  1146. */
  1147. new_mci = get_mci_for_node_id(*socket);
  1148. if (!new_mci) {
  1149. sprintf(msg, "Struct for socket #%u wasn't initialized",
  1150. *socket);
  1151. return -EINVAL;
  1152. }
  1153. mci = new_mci;
  1154. pvt = mci->pvt_info;
  1155. /*
  1156. * Step 2) Get memory channel
  1157. */
  1158. prv = 0;
  1159. if (pvt->info.type == SANDY_BRIDGE)
  1160. pci_ha = pvt->pci_ha0;
  1161. else {
  1162. if (sad_ha)
  1163. pci_ha = pvt->pci_ha1;
  1164. else
  1165. pci_ha = pvt->pci_ha0;
  1166. }
  1167. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  1168. pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], &reg);
  1169. limit = TAD_LIMIT(reg);
  1170. if (limit <= prv) {
  1171. sprintf(msg, "Can't discover the memory channel");
  1172. return -EINVAL;
  1173. }
  1174. if (addr <= limit)
  1175. break;
  1176. prv = limit;
  1177. }
  1178. if (n_tads == MAX_TAD) {
  1179. sprintf(msg, "Can't discover the memory channel");
  1180. return -EINVAL;
  1181. }
  1182. ch_way = TAD_CH(reg) + 1;
  1183. sck_way = TAD_SOCK(reg) + 1;
  1184. if (ch_way == 3)
  1185. idx = addr >> 6;
  1186. else
  1187. idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
  1188. idx = idx % ch_way;
  1189. /*
  1190. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  1191. */
  1192. switch (idx) {
  1193. case 0:
  1194. base_ch = TAD_TGT0(reg);
  1195. break;
  1196. case 1:
  1197. base_ch = TAD_TGT1(reg);
  1198. break;
  1199. case 2:
  1200. base_ch = TAD_TGT2(reg);
  1201. break;
  1202. case 3:
  1203. base_ch = TAD_TGT3(reg);
  1204. break;
  1205. default:
  1206. sprintf(msg, "Can't discover the TAD target");
  1207. return -EINVAL;
  1208. }
  1209. *channel_mask = 1 << base_ch;
  1210. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1211. tad_ch_nilv_offset[n_tads],
  1212. &tad_offset);
  1213. if (pvt->is_mirrored) {
  1214. *channel_mask |= 1 << ((base_ch + 2) % 4);
  1215. switch(ch_way) {
  1216. case 2:
  1217. case 4:
  1218. sck_xch = 1 << sck_way * (ch_way >> 1);
  1219. break;
  1220. default:
  1221. sprintf(msg, "Invalid mirror set. Can't decode addr");
  1222. return -EINVAL;
  1223. }
  1224. } else
  1225. sck_xch = (1 << sck_way) * ch_way;
  1226. if (pvt->is_lockstep)
  1227. *channel_mask |= 1 << ((base_ch + 1) % 4);
  1228. offset = TAD_OFFSET(tad_offset);
  1229. 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",
  1230. n_tads,
  1231. addr,
  1232. limit,
  1233. (u32)TAD_SOCK(reg),
  1234. ch_way,
  1235. offset,
  1236. idx,
  1237. base_ch,
  1238. *channel_mask);
  1239. /* Calculate channel address */
  1240. /* Remove the TAD offset */
  1241. if (offset > addr) {
  1242. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  1243. offset, addr);
  1244. return -EINVAL;
  1245. }
  1246. addr -= offset;
  1247. /* Store the low bits [0:6] of the addr */
  1248. ch_addr = addr & 0x7f;
  1249. /* Remove socket wayness and remove 6 bits */
  1250. addr >>= 6;
  1251. addr = div_u64(addr, sck_xch);
  1252. #if 0
  1253. /* Divide by channel way */
  1254. addr = addr / ch_way;
  1255. #endif
  1256. /* Recover the last 6 bits */
  1257. ch_addr |= addr << 6;
  1258. /*
  1259. * Step 3) Decode rank
  1260. */
  1261. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  1262. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1263. rir_way_limit[n_rir],
  1264. &reg);
  1265. if (!IS_RIR_VALID(reg))
  1266. continue;
  1267. limit = pvt->info.rir_limit(reg);
  1268. gb = div_u64_rem(limit >> 20, 1024, &mb);
  1269. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  1270. n_rir,
  1271. gb, (mb*1000)/1024,
  1272. limit,
  1273. 1 << RIR_WAY(reg));
  1274. if (ch_addr <= limit)
  1275. break;
  1276. }
  1277. if (n_rir == MAX_RIR_RANGES) {
  1278. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  1279. ch_addr);
  1280. return -EINVAL;
  1281. }
  1282. rir_way = RIR_WAY(reg);
  1283. if (pvt->is_close_pg)
  1284. idx = (ch_addr >> 6);
  1285. else
  1286. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  1287. idx %= 1 << rir_way;
  1288. pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
  1289. rir_offset[n_rir][idx],
  1290. &reg);
  1291. *rank = RIR_RNK_TGT(reg);
  1292. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  1293. n_rir,
  1294. ch_addr,
  1295. limit,
  1296. rir_way,
  1297. idx);
  1298. return 0;
  1299. }
  1300. /****************************************************************************
  1301. Device initialization routines: put/get, init/exit
  1302. ****************************************************************************/
  1303. /*
  1304. * sbridge_put_all_devices 'put' all the devices that we have
  1305. * reserved via 'get'
  1306. */
  1307. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  1308. {
  1309. int i;
  1310. edac_dbg(0, "\n");
  1311. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1312. struct pci_dev *pdev = sbridge_dev->pdev[i];
  1313. if (!pdev)
  1314. continue;
  1315. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  1316. pdev->bus->number,
  1317. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1318. pci_dev_put(pdev);
  1319. }
  1320. }
  1321. static void sbridge_put_all_devices(void)
  1322. {
  1323. struct sbridge_dev *sbridge_dev, *tmp;
  1324. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  1325. sbridge_put_devices(sbridge_dev);
  1326. free_sbridge_dev(sbridge_dev);
  1327. }
  1328. }
  1329. static int sbridge_get_onedevice(struct pci_dev **prev,
  1330. u8 *num_mc,
  1331. const struct pci_id_table *table,
  1332. const unsigned devno)
  1333. {
  1334. struct sbridge_dev *sbridge_dev;
  1335. const struct pci_id_descr *dev_descr = &table->descr[devno];
  1336. struct pci_dev *pdev = NULL;
  1337. u8 bus = 0;
  1338. sbridge_printk(KERN_DEBUG,
  1339. "Seeking for: PCI ID %04x:%04x\n",
  1340. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1341. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1342. dev_descr->dev_id, *prev);
  1343. if (!pdev) {
  1344. if (*prev) {
  1345. *prev = pdev;
  1346. return 0;
  1347. }
  1348. if (dev_descr->optional)
  1349. return 0;
  1350. /* if the HA wasn't found */
  1351. if (devno == 0)
  1352. return -ENODEV;
  1353. sbridge_printk(KERN_INFO,
  1354. "Device not found: %04x:%04x\n",
  1355. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1356. /* End of list, leave */
  1357. return -ENODEV;
  1358. }
  1359. bus = pdev->bus->number;
  1360. sbridge_dev = get_sbridge_dev(bus);
  1361. if (!sbridge_dev) {
  1362. sbridge_dev = alloc_sbridge_dev(bus, table);
  1363. if (!sbridge_dev) {
  1364. pci_dev_put(pdev);
  1365. return -ENOMEM;
  1366. }
  1367. (*num_mc)++;
  1368. }
  1369. if (sbridge_dev->pdev[devno]) {
  1370. sbridge_printk(KERN_ERR,
  1371. "Duplicated device for %04x:%04x\n",
  1372. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1373. pci_dev_put(pdev);
  1374. return -ENODEV;
  1375. }
  1376. sbridge_dev->pdev[devno] = pdev;
  1377. /* Be sure that the device is enabled */
  1378. if (unlikely(pci_enable_device(pdev) < 0)) {
  1379. sbridge_printk(KERN_ERR,
  1380. "Couldn't enable %04x:%04x\n",
  1381. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1382. return -ENODEV;
  1383. }
  1384. edac_dbg(0, "Detected %04x:%04x\n",
  1385. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1386. /*
  1387. * As stated on drivers/pci/search.c, the reference count for
  1388. * @from is always decremented if it is not %NULL. So, as we need
  1389. * to get all devices up to null, we need to do a get for the device
  1390. */
  1391. pci_dev_get(pdev);
  1392. *prev = pdev;
  1393. return 0;
  1394. }
  1395. /*
  1396. * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
  1397. * devices we want to reference for this driver.
  1398. * @num_mc: pointer to the memory controllers count, to be incremented in case
  1399. * of success.
  1400. * @table: model specific table
  1401. *
  1402. * returns 0 in case of success or error code
  1403. */
  1404. static int sbridge_get_all_devices(u8 *num_mc,
  1405. const struct pci_id_table *table)
  1406. {
  1407. int i, rc;
  1408. struct pci_dev *pdev = NULL;
  1409. while (table && table->descr) {
  1410. for (i = 0; i < table->n_devs; i++) {
  1411. pdev = NULL;
  1412. do {
  1413. rc = sbridge_get_onedevice(&pdev, num_mc,
  1414. table, i);
  1415. if (rc < 0) {
  1416. if (i == 0) {
  1417. i = table->n_devs;
  1418. break;
  1419. }
  1420. sbridge_put_all_devices();
  1421. return -ENODEV;
  1422. }
  1423. } while (pdev);
  1424. }
  1425. table++;
  1426. }
  1427. return 0;
  1428. }
  1429. static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
  1430. struct sbridge_dev *sbridge_dev)
  1431. {
  1432. struct sbridge_pvt *pvt = mci->pvt_info;
  1433. struct pci_dev *pdev;
  1434. int i;
  1435. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1436. pdev = sbridge_dev->pdev[i];
  1437. if (!pdev)
  1438. continue;
  1439. switch (pdev->device) {
  1440. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
  1441. pvt->pci_sad0 = pdev;
  1442. break;
  1443. case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
  1444. pvt->pci_sad1 = pdev;
  1445. break;
  1446. case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
  1447. pvt->pci_br0 = pdev;
  1448. break;
  1449. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  1450. pvt->pci_ha0 = pdev;
  1451. break;
  1452. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
  1453. pvt->pci_ta = pdev;
  1454. break;
  1455. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
  1456. pvt->pci_ras = pdev;
  1457. break;
  1458. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
  1459. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
  1460. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
  1461. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
  1462. {
  1463. int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
  1464. pvt->pci_tad[id] = pdev;
  1465. }
  1466. break;
  1467. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
  1468. pvt->pci_ddrio = pdev;
  1469. break;
  1470. default:
  1471. goto error;
  1472. }
  1473. edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
  1474. pdev->vendor, pdev->device,
  1475. sbridge_dev->bus,
  1476. pdev);
  1477. }
  1478. /* Check if everything were registered */
  1479. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  1480. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
  1481. goto enodev;
  1482. for (i = 0; i < NUM_CHANNELS; i++) {
  1483. if (!pvt->pci_tad[i])
  1484. goto enodev;
  1485. }
  1486. return 0;
  1487. enodev:
  1488. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1489. return -ENODEV;
  1490. error:
  1491. sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
  1492. PCI_VENDOR_ID_INTEL, pdev->device);
  1493. return -EINVAL;
  1494. }
  1495. static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
  1496. struct sbridge_dev *sbridge_dev)
  1497. {
  1498. struct sbridge_pvt *pvt = mci->pvt_info;
  1499. struct pci_dev *pdev;
  1500. u8 saw_chan_mask = 0;
  1501. int i;
  1502. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1503. pdev = sbridge_dev->pdev[i];
  1504. if (!pdev)
  1505. continue;
  1506. switch (pdev->device) {
  1507. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
  1508. pvt->pci_ha0 = pdev;
  1509. break;
  1510. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  1511. pvt->pci_ta = pdev;
  1512. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
  1513. pvt->pci_ras = pdev;
  1514. break;
  1515. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
  1516. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
  1517. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
  1518. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
  1519. {
  1520. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
  1521. pvt->pci_tad[id] = pdev;
  1522. saw_chan_mask |= 1 << id;
  1523. }
  1524. break;
  1525. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
  1526. pvt->pci_ddrio = pdev;
  1527. break;
  1528. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
  1529. pvt->pci_ddrio = pdev;
  1530. break;
  1531. case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
  1532. pvt->pci_sad0 = pdev;
  1533. break;
  1534. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
  1535. pvt->pci_br0 = pdev;
  1536. break;
  1537. case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
  1538. pvt->pci_br1 = pdev;
  1539. break;
  1540. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
  1541. pvt->pci_ha1 = pdev;
  1542. break;
  1543. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
  1544. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
  1545. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
  1546. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
  1547. {
  1548. int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
  1549. pvt->pci_tad[id] = pdev;
  1550. saw_chan_mask |= 1 << id;
  1551. }
  1552. break;
  1553. default:
  1554. goto error;
  1555. }
  1556. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1557. sbridge_dev->bus,
  1558. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1559. pdev);
  1560. }
  1561. /* Check if everything were registered */
  1562. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
  1563. !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
  1564. !pvt->pci_ta)
  1565. goto enodev;
  1566. if (saw_chan_mask != 0x0f && /* -EN */
  1567. saw_chan_mask != 0x33 && /* -EP */
  1568. saw_chan_mask != 0xff) /* -EX */
  1569. goto enodev;
  1570. return 0;
  1571. enodev:
  1572. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1573. return -ENODEV;
  1574. error:
  1575. sbridge_printk(KERN_ERR,
  1576. "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
  1577. pdev->device);
  1578. return -EINVAL;
  1579. }
  1580. static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
  1581. struct sbridge_dev *sbridge_dev)
  1582. {
  1583. struct sbridge_pvt *pvt = mci->pvt_info;
  1584. struct pci_dev *pdev;
  1585. u8 saw_chan_mask = 0;
  1586. int i;
  1587. /* there's only one device per system; not tied to any bus */
  1588. if (pvt->info.pci_vtd == NULL)
  1589. /* result will be checked later */
  1590. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  1591. PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
  1592. NULL);
  1593. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1594. pdev = sbridge_dev->pdev[i];
  1595. if (!pdev)
  1596. continue;
  1597. switch (pdev->device) {
  1598. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
  1599. pvt->pci_sad0 = pdev;
  1600. break;
  1601. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
  1602. pvt->pci_sad1 = pdev;
  1603. break;
  1604. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
  1605. pvt->pci_ha0 = pdev;
  1606. break;
  1607. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
  1608. pvt->pci_ta = pdev;
  1609. break;
  1610. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
  1611. pvt->pci_ras = pdev;
  1612. break;
  1613. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
  1614. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
  1615. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
  1616. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
  1617. {
  1618. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
  1619. pvt->pci_tad[id] = pdev;
  1620. saw_chan_mask |= 1 << id;
  1621. }
  1622. break;
  1623. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
  1624. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
  1625. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
  1626. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
  1627. {
  1628. int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
  1629. pvt->pci_tad[id] = pdev;
  1630. saw_chan_mask |= 1 << id;
  1631. }
  1632. break;
  1633. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
  1634. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
  1635. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
  1636. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
  1637. if (!pvt->pci_ddrio)
  1638. pvt->pci_ddrio = pdev;
  1639. break;
  1640. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
  1641. pvt->pci_ha1 = pdev;
  1642. break;
  1643. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
  1644. pvt->pci_ha1_ta = pdev;
  1645. break;
  1646. default:
  1647. break;
  1648. }
  1649. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1650. sbridge_dev->bus,
  1651. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1652. pdev);
  1653. }
  1654. /* Check if everything were registered */
  1655. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  1656. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  1657. goto enodev;
  1658. if (saw_chan_mask != 0x0f && /* -EN */
  1659. saw_chan_mask != 0x33 && /* -EP */
  1660. saw_chan_mask != 0xff) /* -EX */
  1661. goto enodev;
  1662. return 0;
  1663. enodev:
  1664. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1665. return -ENODEV;
  1666. }
  1667. static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
  1668. struct sbridge_dev *sbridge_dev)
  1669. {
  1670. struct sbridge_pvt *pvt = mci->pvt_info;
  1671. struct pci_dev *pdev;
  1672. u8 saw_chan_mask = 0;
  1673. int i;
  1674. /* there's only one device per system; not tied to any bus */
  1675. if (pvt->info.pci_vtd == NULL)
  1676. /* result will be checked later */
  1677. pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
  1678. PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
  1679. NULL);
  1680. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1681. pdev = sbridge_dev->pdev[i];
  1682. if (!pdev)
  1683. continue;
  1684. switch (pdev->device) {
  1685. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
  1686. pvt->pci_sad0 = pdev;
  1687. break;
  1688. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
  1689. pvt->pci_sad1 = pdev;
  1690. break;
  1691. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
  1692. pvt->pci_ha0 = pdev;
  1693. break;
  1694. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
  1695. pvt->pci_ta = pdev;
  1696. break;
  1697. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
  1698. pvt->pci_ras = pdev;
  1699. break;
  1700. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
  1701. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
  1702. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
  1703. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
  1704. {
  1705. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
  1706. pvt->pci_tad[id] = pdev;
  1707. saw_chan_mask |= 1 << id;
  1708. }
  1709. break;
  1710. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
  1711. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
  1712. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
  1713. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
  1714. {
  1715. int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
  1716. pvt->pci_tad[id] = pdev;
  1717. saw_chan_mask |= 1 << id;
  1718. }
  1719. break;
  1720. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
  1721. pvt->pci_ddrio = pdev;
  1722. break;
  1723. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
  1724. pvt->pci_ha1 = pdev;
  1725. break;
  1726. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
  1727. pvt->pci_ha1_ta = pdev;
  1728. break;
  1729. default:
  1730. break;
  1731. }
  1732. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1733. sbridge_dev->bus,
  1734. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1735. pdev);
  1736. }
  1737. /* Check if everything were registered */
  1738. if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
  1739. !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
  1740. goto enodev;
  1741. if (saw_chan_mask != 0x0f && /* -EN */
  1742. saw_chan_mask != 0x33 && /* -EP */
  1743. saw_chan_mask != 0xff) /* -EX */
  1744. goto enodev;
  1745. return 0;
  1746. enodev:
  1747. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1748. return -ENODEV;
  1749. }
  1750. /****************************************************************************
  1751. Error check routines
  1752. ****************************************************************************/
  1753. /*
  1754. * While Sandy Bridge has error count registers, SMI BIOS read values from
  1755. * and resets the counters. So, they are not reliable for the OS to read
  1756. * from them. So, we have no option but to just trust on whatever MCE is
  1757. * telling us about the errors.
  1758. */
  1759. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  1760. const struct mce *m)
  1761. {
  1762. struct mem_ctl_info *new_mci;
  1763. struct sbridge_pvt *pvt = mci->pvt_info;
  1764. enum hw_event_mc_err_type tp_event;
  1765. char *type, *optype, msg[256];
  1766. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  1767. bool overflow = GET_BITFIELD(m->status, 62, 62);
  1768. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  1769. bool recoverable;
  1770. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1771. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1772. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1773. u32 channel = GET_BITFIELD(m->status, 0, 3);
  1774. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1775. long channel_mask, first_channel;
  1776. u8 rank, socket, ha;
  1777. int rc, dimm;
  1778. char *area_type = NULL;
  1779. if (pvt->info.type != SANDY_BRIDGE)
  1780. recoverable = true;
  1781. else
  1782. recoverable = GET_BITFIELD(m->status, 56, 56);
  1783. if (uncorrected_error) {
  1784. if (ripv) {
  1785. type = "FATAL";
  1786. tp_event = HW_EVENT_ERR_FATAL;
  1787. } else {
  1788. type = "NON_FATAL";
  1789. tp_event = HW_EVENT_ERR_UNCORRECTED;
  1790. }
  1791. } else {
  1792. type = "CORRECTED";
  1793. tp_event = HW_EVENT_ERR_CORRECTED;
  1794. }
  1795. /*
  1796. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  1797. * memory errors should fit in this mask:
  1798. * 000f 0000 1mmm cccc (binary)
  1799. * where:
  1800. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1801. * won't be shown
  1802. * mmm = error type
  1803. * cccc = channel
  1804. * If the mask doesn't match, report an error to the parsing logic
  1805. */
  1806. if (! ((errcode & 0xef80) == 0x80)) {
  1807. optype = "Can't parse: it is not a mem";
  1808. } else {
  1809. switch (optypenum) {
  1810. case 0:
  1811. optype = "generic undef request error";
  1812. break;
  1813. case 1:
  1814. optype = "memory read error";
  1815. break;
  1816. case 2:
  1817. optype = "memory write error";
  1818. break;
  1819. case 3:
  1820. optype = "addr/cmd error";
  1821. break;
  1822. case 4:
  1823. optype = "memory scrubbing error";
  1824. break;
  1825. default:
  1826. optype = "reserved";
  1827. break;
  1828. }
  1829. }
  1830. /* Only decode errors with an valid address (ADDRV) */
  1831. if (!GET_BITFIELD(m->status, 58, 58))
  1832. return;
  1833. rc = get_memory_error_data(mci, m->addr, &socket, &ha,
  1834. &channel_mask, &rank, &area_type, msg);
  1835. if (rc < 0)
  1836. goto err_parsing;
  1837. new_mci = get_mci_for_node_id(socket);
  1838. if (!new_mci) {
  1839. strcpy(msg, "Error: socket got corrupted!");
  1840. goto err_parsing;
  1841. }
  1842. mci = new_mci;
  1843. pvt = mci->pvt_info;
  1844. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  1845. if (rank < 4)
  1846. dimm = 0;
  1847. else if (rank < 8)
  1848. dimm = 1;
  1849. else
  1850. dimm = 2;
  1851. /*
  1852. * FIXME: On some memory configurations (mirror, lockstep), the
  1853. * Memory Controller can't point the error to a single DIMM. The
  1854. * EDAC core should be handling the channel mask, in order to point
  1855. * to the group of dimm's where the error may be happening.
  1856. */
  1857. if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
  1858. channel = first_channel;
  1859. snprintf(msg, sizeof(msg),
  1860. "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
  1861. overflow ? " OVERFLOW" : "",
  1862. (uncorrected_error && recoverable) ? " recoverable" : "",
  1863. area_type,
  1864. mscod, errcode,
  1865. socket, ha,
  1866. channel_mask,
  1867. rank);
  1868. edac_dbg(0, "%s\n", msg);
  1869. /* FIXME: need support for channel mask */
  1870. if (channel == CHANNEL_UNSPECIFIED)
  1871. channel = -1;
  1872. /* Call the helper to output message */
  1873. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  1874. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  1875. 4*ha+channel, dimm, -1,
  1876. optype, msg);
  1877. return;
  1878. err_parsing:
  1879. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
  1880. -1, -1, -1,
  1881. msg, "");
  1882. }
  1883. /*
  1884. * sbridge_check_error Retrieve and process errors reported by the
  1885. * hardware. Called by the Core module.
  1886. */
  1887. static void sbridge_check_error(struct mem_ctl_info *mci)
  1888. {
  1889. struct sbridge_pvt *pvt = mci->pvt_info;
  1890. int i;
  1891. unsigned count = 0;
  1892. struct mce *m;
  1893. /*
  1894. * MCE first step: Copy all mce errors into a temporary buffer
  1895. * We use a double buffering here, to reduce the risk of
  1896. * loosing an error.
  1897. */
  1898. smp_rmb();
  1899. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1900. % MCE_LOG_LEN;
  1901. if (!count)
  1902. return;
  1903. m = pvt->mce_outentry;
  1904. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1905. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1906. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1907. smp_wmb();
  1908. pvt->mce_in = 0;
  1909. count -= l;
  1910. m += l;
  1911. }
  1912. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1913. smp_wmb();
  1914. pvt->mce_in += count;
  1915. smp_rmb();
  1916. if (pvt->mce_overrun) {
  1917. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  1918. pvt->mce_overrun);
  1919. smp_wmb();
  1920. pvt->mce_overrun = 0;
  1921. }
  1922. /*
  1923. * MCE second step: parse errors and display
  1924. */
  1925. for (i = 0; i < count; i++)
  1926. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  1927. }
  1928. /*
  1929. * sbridge_mce_check_error Replicates mcelog routine to get errors
  1930. * This routine simply queues mcelog errors, and
  1931. * return. The error itself should be handled later
  1932. * by sbridge_check_error.
  1933. * WARNING: As this routine should be called at NMI time, extra care should
  1934. * be taken to avoid deadlocks, and to be as fast as possible.
  1935. */
  1936. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  1937. void *data)
  1938. {
  1939. struct mce *mce = (struct mce *)data;
  1940. struct mem_ctl_info *mci;
  1941. struct sbridge_pvt *pvt;
  1942. char *type;
  1943. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  1944. return NOTIFY_DONE;
  1945. mci = get_mci_for_node_id(mce->socketid);
  1946. if (!mci)
  1947. return NOTIFY_BAD;
  1948. pvt = mci->pvt_info;
  1949. /*
  1950. * Just let mcelog handle it if the error is
  1951. * outside the memory controller. A memory error
  1952. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1953. * bit 12 has an special meaning.
  1954. */
  1955. if ((mce->status & 0xefff) >> 7 != 1)
  1956. return NOTIFY_DONE;
  1957. if (mce->mcgstatus & MCG_STATUS_MCIP)
  1958. type = "Exception";
  1959. else
  1960. type = "Event";
  1961. sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
  1962. sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
  1963. "Bank %d: %016Lx\n", mce->extcpu, type,
  1964. mce->mcgstatus, mce->bank, mce->status);
  1965. sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
  1966. sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
  1967. sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
  1968. sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
  1969. "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
  1970. mce->time, mce->socketid, mce->apicid);
  1971. smp_rmb();
  1972. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1973. smp_wmb();
  1974. pvt->mce_overrun++;
  1975. return NOTIFY_DONE;
  1976. }
  1977. /* Copy memory error at the ringbuffer */
  1978. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1979. smp_wmb();
  1980. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1981. /* Handle fatal errors immediately */
  1982. if (mce->mcgstatus & 1)
  1983. sbridge_check_error(mci);
  1984. /* Advice mcelog that the error were handled */
  1985. return NOTIFY_STOP;
  1986. }
  1987. static struct notifier_block sbridge_mce_dec = {
  1988. .notifier_call = sbridge_mce_check_error,
  1989. };
  1990. /****************************************************************************
  1991. EDAC register/unregister logic
  1992. ****************************************************************************/
  1993. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1994. {
  1995. struct mem_ctl_info *mci = sbridge_dev->mci;
  1996. struct sbridge_pvt *pvt;
  1997. if (unlikely(!mci || !mci->pvt_info)) {
  1998. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  1999. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  2000. return;
  2001. }
  2002. pvt = mci->pvt_info;
  2003. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2004. mci, &sbridge_dev->pdev[0]->dev);
  2005. /* Remove MC sysfs nodes */
  2006. edac_mc_del_mc(mci->pdev);
  2007. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  2008. kfree(mci->ctl_name);
  2009. edac_mc_free(mci);
  2010. sbridge_dev->mci = NULL;
  2011. }
  2012. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
  2013. {
  2014. struct mem_ctl_info *mci;
  2015. struct edac_mc_layer layers[2];
  2016. struct sbridge_pvt *pvt;
  2017. struct pci_dev *pdev = sbridge_dev->pdev[0];
  2018. int rc;
  2019. /* Check the number of active and not disabled channels */
  2020. rc = check_if_ecc_is_active(sbridge_dev->bus, type);
  2021. if (unlikely(rc < 0))
  2022. return rc;
  2023. /* allocate a new MC control structure */
  2024. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  2025. layers[0].size = NUM_CHANNELS;
  2026. layers[0].is_virt_csrow = false;
  2027. layers[1].type = EDAC_MC_LAYER_SLOT;
  2028. layers[1].size = MAX_DIMMS;
  2029. layers[1].is_virt_csrow = true;
  2030. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  2031. sizeof(*pvt));
  2032. if (unlikely(!mci))
  2033. return -ENOMEM;
  2034. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  2035. mci, &pdev->dev);
  2036. pvt = mci->pvt_info;
  2037. memset(pvt, 0, sizeof(*pvt));
  2038. /* Associate sbridge_dev and mci for future usage */
  2039. pvt->sbridge_dev = sbridge_dev;
  2040. sbridge_dev->mci = mci;
  2041. mci->mtype_cap = MEM_FLAG_DDR3;
  2042. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  2043. mci->edac_cap = EDAC_FLAG_NONE;
  2044. mci->mod_name = "sbridge_edac.c";
  2045. mci->mod_ver = SBRIDGE_REVISION;
  2046. mci->dev_name = pci_name(pdev);
  2047. mci->ctl_page_to_phys = NULL;
  2048. /* Set the function pointer to an actual operation function */
  2049. mci->edac_check = sbridge_check_error;
  2050. pvt->info.type = type;
  2051. switch (type) {
  2052. case IVY_BRIDGE:
  2053. pvt->info.rankcfgr = IB_RANK_CFG_A;
  2054. pvt->info.get_tolm = ibridge_get_tolm;
  2055. pvt->info.get_tohm = ibridge_get_tohm;
  2056. pvt->info.dram_rule = ibridge_dram_rule;
  2057. pvt->info.get_memory_type = get_memory_type;
  2058. pvt->info.get_node_id = get_node_id;
  2059. pvt->info.rir_limit = rir_limit;
  2060. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2061. pvt->info.interleave_list = ibridge_interleave_list;
  2062. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2063. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2064. pvt->info.get_width = ibridge_get_width;
  2065. mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
  2066. /* Store pci devices at mci for faster access */
  2067. rc = ibridge_mci_bind_devs(mci, sbridge_dev);
  2068. if (unlikely(rc < 0))
  2069. goto fail0;
  2070. break;
  2071. case SANDY_BRIDGE:
  2072. pvt->info.rankcfgr = SB_RANK_CFG_A;
  2073. pvt->info.get_tolm = sbridge_get_tolm;
  2074. pvt->info.get_tohm = sbridge_get_tohm;
  2075. pvt->info.dram_rule = sbridge_dram_rule;
  2076. pvt->info.get_memory_type = get_memory_type;
  2077. pvt->info.get_node_id = get_node_id;
  2078. pvt->info.rir_limit = rir_limit;
  2079. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  2080. pvt->info.interleave_list = sbridge_interleave_list;
  2081. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  2082. pvt->info.interleave_pkg = sbridge_interleave_pkg;
  2083. pvt->info.get_width = sbridge_get_width;
  2084. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  2085. /* Store pci devices at mci for faster access */
  2086. rc = sbridge_mci_bind_devs(mci, sbridge_dev);
  2087. if (unlikely(rc < 0))
  2088. goto fail0;
  2089. break;
  2090. case HASWELL:
  2091. /* rankcfgr isn't used */
  2092. pvt->info.get_tolm = haswell_get_tolm;
  2093. pvt->info.get_tohm = haswell_get_tohm;
  2094. pvt->info.dram_rule = ibridge_dram_rule;
  2095. pvt->info.get_memory_type = haswell_get_memory_type;
  2096. pvt->info.get_node_id = haswell_get_node_id;
  2097. pvt->info.rir_limit = haswell_rir_limit;
  2098. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2099. pvt->info.interleave_list = ibridge_interleave_list;
  2100. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2101. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2102. pvt->info.get_width = ibridge_get_width;
  2103. mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
  2104. /* Store pci devices at mci for faster access */
  2105. rc = haswell_mci_bind_devs(mci, sbridge_dev);
  2106. if (unlikely(rc < 0))
  2107. goto fail0;
  2108. break;
  2109. case BROADWELL:
  2110. /* rankcfgr isn't used */
  2111. pvt->info.get_tolm = haswell_get_tolm;
  2112. pvt->info.get_tohm = haswell_get_tohm;
  2113. pvt->info.dram_rule = ibridge_dram_rule;
  2114. pvt->info.get_memory_type = haswell_get_memory_type;
  2115. pvt->info.get_node_id = haswell_get_node_id;
  2116. pvt->info.rir_limit = haswell_rir_limit;
  2117. pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
  2118. pvt->info.interleave_list = ibridge_interleave_list;
  2119. pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
  2120. pvt->info.interleave_pkg = ibridge_interleave_pkg;
  2121. pvt->info.get_width = broadwell_get_width;
  2122. mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
  2123. /* Store pci devices at mci for faster access */
  2124. rc = broadwell_mci_bind_devs(mci, sbridge_dev);
  2125. if (unlikely(rc < 0))
  2126. goto fail0;
  2127. break;
  2128. }
  2129. /* Get dimm basic config and the memory layout */
  2130. get_dimm_config(mci);
  2131. get_memory_layout(mci);
  2132. /* record ptr to the generic device */
  2133. mci->pdev = &pdev->dev;
  2134. /* add this new MC control structure to EDAC's list of MCs */
  2135. if (unlikely(edac_mc_add_mc(mci))) {
  2136. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  2137. rc = -EINVAL;
  2138. goto fail0;
  2139. }
  2140. return 0;
  2141. fail0:
  2142. kfree(mci->ctl_name);
  2143. edac_mc_free(mci);
  2144. sbridge_dev->mci = NULL;
  2145. return rc;
  2146. }
  2147. /*
  2148. * sbridge_probe Probe for ONE instance of device to see if it is
  2149. * present.
  2150. * return:
  2151. * 0 for FOUND a device
  2152. * < 0 for error code
  2153. */
  2154. static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2155. {
  2156. int rc = -ENODEV;
  2157. u8 mc, num_mc = 0;
  2158. struct sbridge_dev *sbridge_dev;
  2159. enum type type = SANDY_BRIDGE;
  2160. /* get the pci devices we want to reserve for our use */
  2161. mutex_lock(&sbridge_edac_lock);
  2162. /*
  2163. * All memory controllers are allocated at the first pass.
  2164. */
  2165. if (unlikely(probed >= 1)) {
  2166. mutex_unlock(&sbridge_edac_lock);
  2167. return -ENODEV;
  2168. }
  2169. probed++;
  2170. switch (pdev->device) {
  2171. case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
  2172. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
  2173. type = IVY_BRIDGE;
  2174. break;
  2175. case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
  2176. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
  2177. type = SANDY_BRIDGE;
  2178. break;
  2179. case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
  2180. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_haswell_table);
  2181. type = HASWELL;
  2182. break;
  2183. case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
  2184. rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_broadwell_table);
  2185. type = BROADWELL;
  2186. break;
  2187. }
  2188. if (unlikely(rc < 0)) {
  2189. edac_dbg(0, "couldn't get all devices for 0x%x\n", pdev->device);
  2190. goto fail0;
  2191. }
  2192. mc = 0;
  2193. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  2194. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  2195. mc, mc + 1, num_mc);
  2196. sbridge_dev->mc = mc++;
  2197. rc = sbridge_register_mci(sbridge_dev, type);
  2198. if (unlikely(rc < 0))
  2199. goto fail1;
  2200. }
  2201. sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
  2202. mutex_unlock(&sbridge_edac_lock);
  2203. return 0;
  2204. fail1:
  2205. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2206. sbridge_unregister_mci(sbridge_dev);
  2207. sbridge_put_all_devices();
  2208. fail0:
  2209. mutex_unlock(&sbridge_edac_lock);
  2210. return rc;
  2211. }
  2212. /*
  2213. * sbridge_remove destructor for one instance of device
  2214. *
  2215. */
  2216. static void sbridge_remove(struct pci_dev *pdev)
  2217. {
  2218. struct sbridge_dev *sbridge_dev;
  2219. edac_dbg(0, "\n");
  2220. /*
  2221. * we have a trouble here: pdev value for removal will be wrong, since
  2222. * it will point to the X58 register used to detect that the machine
  2223. * is a Nehalem or upper design. However, due to the way several PCI
  2224. * devices are grouped together to provide MC functionality, we need
  2225. * to use a different method for releasing the devices
  2226. */
  2227. mutex_lock(&sbridge_edac_lock);
  2228. if (unlikely(!probed)) {
  2229. mutex_unlock(&sbridge_edac_lock);
  2230. return;
  2231. }
  2232. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  2233. sbridge_unregister_mci(sbridge_dev);
  2234. /* Release PCI resources */
  2235. sbridge_put_all_devices();
  2236. probed--;
  2237. mutex_unlock(&sbridge_edac_lock);
  2238. }
  2239. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  2240. /*
  2241. * sbridge_driver pci_driver structure for this module
  2242. *
  2243. */
  2244. static struct pci_driver sbridge_driver = {
  2245. .name = "sbridge_edac",
  2246. .probe = sbridge_probe,
  2247. .remove = sbridge_remove,
  2248. .id_table = sbridge_pci_tbl,
  2249. };
  2250. /*
  2251. * sbridge_init Module entry function
  2252. * Try to initialize this module for its devices
  2253. */
  2254. static int __init sbridge_init(void)
  2255. {
  2256. int pci_rc;
  2257. edac_dbg(2, "\n");
  2258. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  2259. opstate_init();
  2260. pci_rc = pci_register_driver(&sbridge_driver);
  2261. if (pci_rc >= 0) {
  2262. mce_register_decode_chain(&sbridge_mce_dec);
  2263. if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
  2264. sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
  2265. return 0;
  2266. }
  2267. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  2268. pci_rc);
  2269. return pci_rc;
  2270. }
  2271. /*
  2272. * sbridge_exit() Module exit function
  2273. * Unregister the driver
  2274. */
  2275. static void __exit sbridge_exit(void)
  2276. {
  2277. edac_dbg(2, "\n");
  2278. pci_unregister_driver(&sbridge_driver);
  2279. mce_unregister_decode_chain(&sbridge_mce_dec);
  2280. }
  2281. module_init(sbridge_init);
  2282. module_exit(sbridge_exit);
  2283. module_param(edac_op_state, int, 0444);
  2284. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  2285. MODULE_LICENSE("GPL");
  2286. MODULE_AUTHOR("Mauro Carvalho Chehab");
  2287. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  2288. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "
  2289. SBRIDGE_REVISION);