sb_edac.c 62 KB

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