i7core_edac.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. /* Intel 7 core Memory Controller kernel module (Nehalem)
  2. *
  3. * This file may be distributed under the terms of the
  4. * GNU General Public License version 2 only.
  5. *
  6. * Copyright (c) 2009 by:
  7. * Mauro Carvalho Chehab <mchehab@redhat.com>
  8. *
  9. * Red Hat Inc. http://www.redhat.com
  10. *
  11. * Forked and adapted from the i5400_edac driver
  12. *
  13. * Based on the following public Intel datasheets:
  14. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  15. * Datasheet, Volume 2:
  16. * http://download.intel.com/design/processor/datashts/320835.pdf
  17. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  18. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  19. * also available at:
  20. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci_ids.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/edac.h>
  29. #include <linux/mmzone.h>
  30. #include <linux/edac_mce.h>
  31. #include <linux/smp.h>
  32. #include <asm/processor.h>
  33. #include "edac_core.h"
  34. /*
  35. * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
  36. * registers start at bus 255, and are not reported by BIOS.
  37. * We currently find devices with only 2 sockets. In order to support more QPI
  38. * Quick Path Interconnect, just increment this number.
  39. */
  40. #define MAX_SOCKET_BUSES 2
  41. /*
  42. * Alter this version for the module when modifications are made
  43. */
  44. #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
  45. #define EDAC_MOD_STR "i7core_edac"
  46. /*
  47. * Debug macros
  48. */
  49. #define i7core_printk(level, fmt, arg...) \
  50. edac_printk(level, "i7core", fmt, ##arg)
  51. #define i7core_mc_printk(mci, level, fmt, arg...) \
  52. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  53. /*
  54. * i7core Memory Controller Registers
  55. */
  56. /* OFFSETS for Device 0 Function 0 */
  57. #define MC_CFG_CONTROL 0x90
  58. /* OFFSETS for Device 3 Function 0 */
  59. #define MC_CONTROL 0x48
  60. #define MC_STATUS 0x4c
  61. #define MC_MAX_DOD 0x64
  62. /*
  63. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  64. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  65. */
  66. #define MC_TEST_ERR_RCV1 0x60
  67. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  68. #define MC_TEST_ERR_RCV0 0x64
  69. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  70. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  71. /* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
  72. #define MC_COR_ECC_CNT_0 0x80
  73. #define MC_COR_ECC_CNT_1 0x84
  74. #define MC_COR_ECC_CNT_2 0x88
  75. #define MC_COR_ECC_CNT_3 0x8c
  76. #define MC_COR_ECC_CNT_4 0x90
  77. #define MC_COR_ECC_CNT_5 0x94
  78. #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
  79. #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
  80. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  81. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  82. #define THREE_DIMMS_PRESENT (1 << 24)
  83. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  84. #define QUAD_RANK_PRESENT (1 << 22)
  85. #define REGISTERED_DIMM (1 << 15)
  86. #define MC_CHANNEL_MAPPER 0x60
  87. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  88. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  89. #define MC_CHANNEL_RANK_PRESENT 0x7c
  90. #define RANK_PRESENT_MASK 0xffff
  91. #define MC_CHANNEL_ADDR_MATCH 0xf0
  92. #define MC_CHANNEL_ERROR_MASK 0xf8
  93. #define MC_CHANNEL_ERROR_INJECT 0xfc
  94. #define INJECT_ADDR_PARITY 0x10
  95. #define INJECT_ECC 0x08
  96. #define MASK_CACHELINE 0x06
  97. #define MASK_FULL_CACHELINE 0x06
  98. #define MASK_MSB32_CACHELINE 0x04
  99. #define MASK_LSB32_CACHELINE 0x02
  100. #define NO_MASK_CACHELINE 0x00
  101. #define REPEAT_EN 0x01
  102. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  103. #define MC_DOD_CH_DIMM0 0x48
  104. #define MC_DOD_CH_DIMM1 0x4c
  105. #define MC_DOD_CH_DIMM2 0x50
  106. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  107. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  108. #define DIMM_PRESENT_MASK (1 << 9)
  109. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  110. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  111. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  112. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  113. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  114. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  115. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  116. #define MC_DOD_NUMCOL_MASK 3
  117. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  118. #define MC_RANK_PRESENT 0x7c
  119. #define MC_SAG_CH_0 0x80
  120. #define MC_SAG_CH_1 0x84
  121. #define MC_SAG_CH_2 0x88
  122. #define MC_SAG_CH_3 0x8c
  123. #define MC_SAG_CH_4 0x90
  124. #define MC_SAG_CH_5 0x94
  125. #define MC_SAG_CH_6 0x98
  126. #define MC_SAG_CH_7 0x9c
  127. #define MC_RIR_LIMIT_CH_0 0x40
  128. #define MC_RIR_LIMIT_CH_1 0x44
  129. #define MC_RIR_LIMIT_CH_2 0x48
  130. #define MC_RIR_LIMIT_CH_3 0x4C
  131. #define MC_RIR_LIMIT_CH_4 0x50
  132. #define MC_RIR_LIMIT_CH_5 0x54
  133. #define MC_RIR_LIMIT_CH_6 0x58
  134. #define MC_RIR_LIMIT_CH_7 0x5C
  135. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  136. #define MC_RIR_WAY_CH 0x80
  137. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  138. #define MC_RIR_WAY_RANK_MASK 0x7
  139. /*
  140. * i7core structs
  141. */
  142. #define NUM_CHANS 3
  143. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  144. #define MAX_MCR_FUNC 4
  145. #define MAX_CHAN_FUNC 3
  146. struct i7core_info {
  147. u32 mc_control;
  148. u32 mc_status;
  149. u32 max_dod;
  150. u32 ch_map;
  151. };
  152. struct i7core_inject {
  153. int enable;
  154. u32 section;
  155. u32 type;
  156. u32 eccmask;
  157. /* Error address mask */
  158. int channel, dimm, rank, bank, page, col;
  159. };
  160. struct i7core_channel {
  161. u32 ranks;
  162. u32 dimms;
  163. };
  164. struct pci_id_descr {
  165. int dev;
  166. int func;
  167. int dev_id;
  168. int optional;
  169. };
  170. struct i7core_dev {
  171. struct list_head list;
  172. u8 socket;
  173. struct pci_dev **pdev;
  174. int n_devs;
  175. struct mem_ctl_info *mci;
  176. };
  177. struct i7core_pvt {
  178. struct pci_dev *pci_noncore;
  179. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  180. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  181. struct i7core_dev *i7core_dev;
  182. struct i7core_info info;
  183. struct i7core_inject inject;
  184. struct i7core_channel channel[NUM_CHANS];
  185. int channels; /* Number of active channels */
  186. int ce_count_available;
  187. int csrow_map[NUM_CHANS][MAX_DIMMS];
  188. /* ECC corrected errors counts per udimm */
  189. unsigned long udimm_ce_count[MAX_DIMMS];
  190. int udimm_last_ce_count[MAX_DIMMS];
  191. /* ECC corrected errors counts per rdimm */
  192. unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
  193. int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
  194. unsigned int is_registered;
  195. /* mcelog glue */
  196. struct edac_mce edac_mce;
  197. /* Fifo double buffers */
  198. struct mce mce_entry[MCE_LOG_LEN];
  199. struct mce mce_outentry[MCE_LOG_LEN];
  200. /* Fifo in/out counters */
  201. unsigned mce_in, mce_out;
  202. /* Count indicator to show errors not got */
  203. unsigned mce_overrun;
  204. };
  205. /* Static vars */
  206. static LIST_HEAD(i7core_edac_list);
  207. static DEFINE_MUTEX(i7core_edac_lock);
  208. #define PCI_DESCR(device, function, device_id) \
  209. .dev = (device), \
  210. .func = (function), \
  211. .dev_id = (device_id)
  212. struct pci_id_descr pci_dev_descr_i7core[] = {
  213. /* Memory controller */
  214. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  215. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  216. /* Exists only for RDIMM */
  217. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
  218. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  219. /* Channel 0 */
  220. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  221. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  222. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  223. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  224. /* Channel 1 */
  225. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  226. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  227. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  228. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  229. /* Channel 2 */
  230. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  231. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  232. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  233. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  234. /* Generic Non-core registers */
  235. /*
  236. * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
  237. * On Xeon 55xx, however, it has a different id (8086:2c40). So,
  238. * the probing code needs to test for the other address in case of
  239. * failure of this one
  240. */
  241. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
  242. };
  243. struct pci_id_descr pci_dev_descr_lynnfield[] = {
  244. { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
  245. { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
  246. { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
  247. { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
  248. { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
  249. { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
  250. { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
  251. { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
  252. { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
  253. { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
  254. { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
  255. /*
  256. * This is the PCI device has an alternate address on some
  257. * processors like Core i7 860
  258. */
  259. { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
  260. };
  261. /*
  262. * pci_device_id table for which devices we are looking for
  263. */
  264. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  265. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  266. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
  267. {0,} /* 0 terminated list. */
  268. };
  269. static struct edac_pci_ctl_info *i7core_pci;
  270. /****************************************************************************
  271. Anciliary status routines
  272. ****************************************************************************/
  273. /* MC_CONTROL bits */
  274. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  275. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  276. /* MC_STATUS bits */
  277. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
  278. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  279. /* MC_MAX_DOD read functions */
  280. static inline int numdimms(u32 dimms)
  281. {
  282. return (dimms & 0x3) + 1;
  283. }
  284. static inline int numrank(u32 rank)
  285. {
  286. static int ranks[4] = { 1, 2, 4, -EINVAL };
  287. return ranks[rank & 0x3];
  288. }
  289. static inline int numbank(u32 bank)
  290. {
  291. static int banks[4] = { 4, 8, 16, -EINVAL };
  292. return banks[bank & 0x3];
  293. }
  294. static inline int numrow(u32 row)
  295. {
  296. static int rows[8] = {
  297. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  298. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  299. };
  300. return rows[row & 0x7];
  301. }
  302. static inline int numcol(u32 col)
  303. {
  304. static int cols[8] = {
  305. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  306. };
  307. return cols[col & 0x3];
  308. }
  309. static struct i7core_dev *get_i7core_dev(u8 socket)
  310. {
  311. struct i7core_dev *i7core_dev;
  312. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  313. if (i7core_dev->socket == socket)
  314. return i7core_dev;
  315. }
  316. return NULL;
  317. }
  318. /****************************************************************************
  319. Memory check routines
  320. ****************************************************************************/
  321. static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
  322. unsigned func)
  323. {
  324. struct i7core_dev *i7core_dev = get_i7core_dev(socket);
  325. int i;
  326. if (!i7core_dev)
  327. return NULL;
  328. for (i = 0; i < i7core_dev->n_devs; i++) {
  329. if (!i7core_dev->pdev[i])
  330. continue;
  331. if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
  332. PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
  333. return i7core_dev->pdev[i];
  334. }
  335. }
  336. return NULL;
  337. }
  338. /**
  339. * i7core_get_active_channels() - gets the number of channels and csrows
  340. * @socket: Quick Path Interconnect socket
  341. * @channels: Number of channels that will be returned
  342. * @csrows: Number of csrows found
  343. *
  344. * Since EDAC core needs to know in advance the number of available channels
  345. * and csrows, in order to allocate memory for csrows/channels, it is needed
  346. * to run two similar steps. At the first step, implemented on this function,
  347. * it checks the number of csrows/channels present at one socket.
  348. * this is used in order to properly allocate the size of mci components.
  349. *
  350. * It should be noticed that none of the current available datasheets explain
  351. * or even mention how csrows are seen by the memory controller. So, we need
  352. * to add a fake description for csrows.
  353. * So, this driver is attributing one DIMM memory for one csrow.
  354. */
  355. static int i7core_get_active_channels(u8 socket, unsigned *channels,
  356. unsigned *csrows)
  357. {
  358. struct pci_dev *pdev = NULL;
  359. int i, j;
  360. u32 status, control;
  361. *channels = 0;
  362. *csrows = 0;
  363. pdev = get_pdev_slot_func(socket, 3, 0);
  364. if (!pdev) {
  365. i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
  366. socket);
  367. return -ENODEV;
  368. }
  369. /* Device 3 function 0 reads */
  370. pci_read_config_dword(pdev, MC_STATUS, &status);
  371. pci_read_config_dword(pdev, MC_CONTROL, &control);
  372. for (i = 0; i < NUM_CHANS; i++) {
  373. u32 dimm_dod[3];
  374. /* Check if the channel is active */
  375. if (!(control & (1 << (8 + i))))
  376. continue;
  377. /* Check if the channel is disabled */
  378. if (status & (1 << i))
  379. continue;
  380. pdev = get_pdev_slot_func(socket, i + 4, 1);
  381. if (!pdev) {
  382. i7core_printk(KERN_ERR, "Couldn't find socket %d "
  383. "fn %d.%d!!!\n",
  384. socket, i + 4, 1);
  385. return -ENODEV;
  386. }
  387. /* Devices 4-6 function 1 */
  388. pci_read_config_dword(pdev,
  389. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  390. pci_read_config_dword(pdev,
  391. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  392. pci_read_config_dword(pdev,
  393. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  394. (*channels)++;
  395. for (j = 0; j < 3; j++) {
  396. if (!DIMM_PRESENT(dimm_dod[j]))
  397. continue;
  398. (*csrows)++;
  399. }
  400. }
  401. debugf0("Number of active channels on socket %d: %d\n",
  402. socket, *channels);
  403. return 0;
  404. }
  405. static int get_dimm_config(struct mem_ctl_info *mci, int *csrow)
  406. {
  407. struct i7core_pvt *pvt = mci->pvt_info;
  408. struct csrow_info *csr;
  409. struct pci_dev *pdev;
  410. int i, j;
  411. unsigned long last_page = 0;
  412. enum edac_type mode;
  413. enum mem_type mtype;
  414. /* Get data from the MC register, function 0 */
  415. pdev = pvt->pci_mcr[0];
  416. if (!pdev)
  417. return -ENODEV;
  418. /* Device 3 function 0 reads */
  419. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  420. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  421. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  422. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  423. debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  424. pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
  425. pvt->info.max_dod, pvt->info.ch_map);
  426. if (ECC_ENABLED(pvt)) {
  427. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  428. if (ECCx8(pvt))
  429. mode = EDAC_S8ECD8ED;
  430. else
  431. mode = EDAC_S4ECD4ED;
  432. } else {
  433. debugf0("ECC disabled\n");
  434. mode = EDAC_NONE;
  435. }
  436. /* FIXME: need to handle the error codes */
  437. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
  438. "x%x x 0x%x\n",
  439. numdimms(pvt->info.max_dod),
  440. numrank(pvt->info.max_dod >> 2),
  441. numbank(pvt->info.max_dod >> 4),
  442. numrow(pvt->info.max_dod >> 6),
  443. numcol(pvt->info.max_dod >> 9));
  444. for (i = 0; i < NUM_CHANS; i++) {
  445. u32 data, dimm_dod[3], value[8];
  446. if (!pvt->pci_ch[i][0])
  447. continue;
  448. if (!CH_ACTIVE(pvt, i)) {
  449. debugf0("Channel %i is not active\n", i);
  450. continue;
  451. }
  452. if (CH_DISABLED(pvt, i)) {
  453. debugf0("Channel %i is disabled\n", i);
  454. continue;
  455. }
  456. /* Devices 4-6 function 0 */
  457. pci_read_config_dword(pvt->pci_ch[i][0],
  458. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  459. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
  460. 4 : 2;
  461. if (data & REGISTERED_DIMM)
  462. mtype = MEM_RDDR3;
  463. else
  464. mtype = MEM_DDR3;
  465. #if 0
  466. if (data & THREE_DIMMS_PRESENT)
  467. pvt->channel[i].dimms = 3;
  468. else if (data & SINGLE_QUAD_RANK_PRESENT)
  469. pvt->channel[i].dimms = 1;
  470. else
  471. pvt->channel[i].dimms = 2;
  472. #endif
  473. /* Devices 4-6 function 1 */
  474. pci_read_config_dword(pvt->pci_ch[i][1],
  475. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  476. pci_read_config_dword(pvt->pci_ch[i][1],
  477. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  478. pci_read_config_dword(pvt->pci_ch[i][1],
  479. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  480. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  481. "%d ranks, %cDIMMs\n",
  482. i,
  483. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  484. data,
  485. pvt->channel[i].ranks,
  486. (data & REGISTERED_DIMM) ? 'R' : 'U');
  487. for (j = 0; j < 3; j++) {
  488. u32 banks, ranks, rows, cols;
  489. u32 size, npages;
  490. if (!DIMM_PRESENT(dimm_dod[j]))
  491. continue;
  492. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  493. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  494. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  495. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  496. /* DDR3 has 8 I/O banks */
  497. size = (rows * cols * banks * ranks) >> (20 - 3);
  498. pvt->channel[i].dimms++;
  499. debugf0("\tdimm %d %d Mb offset: %x, "
  500. "bank: %d, rank: %d, row: %#x, col: %#x\n",
  501. j, size,
  502. RANKOFFSET(dimm_dod[j]),
  503. banks, ranks, rows, cols);
  504. #if PAGE_SHIFT > 20
  505. npages = size >> (PAGE_SHIFT - 20);
  506. #else
  507. npages = size << (20 - PAGE_SHIFT);
  508. #endif
  509. csr = &mci->csrows[*csrow];
  510. csr->first_page = last_page + 1;
  511. last_page += npages;
  512. csr->last_page = last_page;
  513. csr->nr_pages = npages;
  514. csr->page_mask = 0;
  515. csr->grain = 8;
  516. csr->csrow_idx = *csrow;
  517. csr->nr_channels = 1;
  518. csr->channels[0].chan_idx = i;
  519. csr->channels[0].ce_count = 0;
  520. pvt->csrow_map[i][j] = *csrow;
  521. switch (banks) {
  522. case 4:
  523. csr->dtype = DEV_X4;
  524. break;
  525. case 8:
  526. csr->dtype = DEV_X8;
  527. break;
  528. case 16:
  529. csr->dtype = DEV_X16;
  530. break;
  531. default:
  532. csr->dtype = DEV_UNKNOWN;
  533. }
  534. csr->edac_mode = mode;
  535. csr->mtype = mtype;
  536. (*csrow)++;
  537. }
  538. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  539. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  540. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  541. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  542. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  543. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  544. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  545. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  546. debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  547. for (j = 0; j < 8; j++)
  548. debugf1("\t\t%#x\t%#x\t%#x\n",
  549. (value[j] >> 27) & 0x1,
  550. (value[j] >> 24) & 0x7,
  551. (value[j] && ((1 << 24) - 1)));
  552. }
  553. return 0;
  554. }
  555. /****************************************************************************
  556. Error insertion routines
  557. ****************************************************************************/
  558. /* The i7core has independent error injection features per channel.
  559. However, to have a simpler code, we don't allow enabling error injection
  560. on more than one channel.
  561. Also, since a change at an inject parameter will be applied only at enable,
  562. we're disabling error injection on all write calls to the sysfs nodes that
  563. controls the error code injection.
  564. */
  565. static int disable_inject(struct mem_ctl_info *mci)
  566. {
  567. struct i7core_pvt *pvt = mci->pvt_info;
  568. pvt->inject.enable = 0;
  569. if (!pvt->pci_ch[pvt->inject.channel][0])
  570. return -ENODEV;
  571. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  572. MC_CHANNEL_ERROR_INJECT, 0);
  573. return 0;
  574. }
  575. /*
  576. * i7core inject inject.section
  577. *
  578. * accept and store error injection inject.section value
  579. * bit 0 - refers to the lower 32-byte half cacheline
  580. * bit 1 - refers to the upper 32-byte half cacheline
  581. */
  582. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  583. const char *data, size_t count)
  584. {
  585. struct i7core_pvt *pvt = mci->pvt_info;
  586. unsigned long value;
  587. int rc;
  588. if (pvt->inject.enable)
  589. disable_inject(mci);
  590. rc = strict_strtoul(data, 10, &value);
  591. if ((rc < 0) || (value > 3))
  592. return -EIO;
  593. pvt->inject.section = (u32) value;
  594. return count;
  595. }
  596. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  597. char *data)
  598. {
  599. struct i7core_pvt *pvt = mci->pvt_info;
  600. return sprintf(data, "0x%08x\n", pvt->inject.section);
  601. }
  602. /*
  603. * i7core inject.type
  604. *
  605. * accept and store error injection inject.section value
  606. * bit 0 - repeat enable - Enable error repetition
  607. * bit 1 - inject ECC error
  608. * bit 2 - inject parity error
  609. */
  610. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  611. const char *data, size_t count)
  612. {
  613. struct i7core_pvt *pvt = mci->pvt_info;
  614. unsigned long value;
  615. int rc;
  616. if (pvt->inject.enable)
  617. disable_inject(mci);
  618. rc = strict_strtoul(data, 10, &value);
  619. if ((rc < 0) || (value > 7))
  620. return -EIO;
  621. pvt->inject.type = (u32) value;
  622. return count;
  623. }
  624. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  625. char *data)
  626. {
  627. struct i7core_pvt *pvt = mci->pvt_info;
  628. return sprintf(data, "0x%08x\n", pvt->inject.type);
  629. }
  630. /*
  631. * i7core_inject_inject.eccmask_store
  632. *
  633. * The type of error (UE/CE) will depend on the inject.eccmask value:
  634. * Any bits set to a 1 will flip the corresponding ECC bit
  635. * Correctable errors can be injected by flipping 1 bit or the bits within
  636. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  637. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  638. * uncorrectable error to be injected.
  639. */
  640. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  641. const char *data, size_t count)
  642. {
  643. struct i7core_pvt *pvt = mci->pvt_info;
  644. unsigned long value;
  645. int rc;
  646. if (pvt->inject.enable)
  647. disable_inject(mci);
  648. rc = strict_strtoul(data, 10, &value);
  649. if (rc < 0)
  650. return -EIO;
  651. pvt->inject.eccmask = (u32) value;
  652. return count;
  653. }
  654. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  655. char *data)
  656. {
  657. struct i7core_pvt *pvt = mci->pvt_info;
  658. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  659. }
  660. /*
  661. * i7core_addrmatch
  662. *
  663. * The type of error (UE/CE) will depend on the inject.eccmask value:
  664. * Any bits set to a 1 will flip the corresponding ECC bit
  665. * Correctable errors can be injected by flipping 1 bit or the bits within
  666. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  667. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  668. * uncorrectable error to be injected.
  669. */
  670. #define DECLARE_ADDR_MATCH(param, limit) \
  671. static ssize_t i7core_inject_store_##param( \
  672. struct mem_ctl_info *mci, \
  673. const char *data, size_t count) \
  674. { \
  675. struct i7core_pvt *pvt; \
  676. long value; \
  677. int rc; \
  678. \
  679. debugf1("%s()\n", __func__); \
  680. pvt = mci->pvt_info; \
  681. \
  682. if (pvt->inject.enable) \
  683. disable_inject(mci); \
  684. \
  685. if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
  686. value = -1; \
  687. else { \
  688. rc = strict_strtoul(data, 10, &value); \
  689. if ((rc < 0) || (value >= limit)) \
  690. return -EIO; \
  691. } \
  692. \
  693. pvt->inject.param = value; \
  694. \
  695. return count; \
  696. } \
  697. \
  698. static ssize_t i7core_inject_show_##param( \
  699. struct mem_ctl_info *mci, \
  700. char *data) \
  701. { \
  702. struct i7core_pvt *pvt; \
  703. \
  704. pvt = mci->pvt_info; \
  705. debugf1("%s() pvt=%p\n", __func__, pvt); \
  706. if (pvt->inject.param < 0) \
  707. return sprintf(data, "any\n"); \
  708. else \
  709. return sprintf(data, "%d\n", pvt->inject.param);\
  710. }
  711. #define ATTR_ADDR_MATCH(param) \
  712. { \
  713. .attr = { \
  714. .name = #param, \
  715. .mode = (S_IRUGO | S_IWUSR) \
  716. }, \
  717. .show = i7core_inject_show_##param, \
  718. .store = i7core_inject_store_##param, \
  719. }
  720. DECLARE_ADDR_MATCH(channel, 3);
  721. DECLARE_ADDR_MATCH(dimm, 3);
  722. DECLARE_ADDR_MATCH(rank, 4);
  723. DECLARE_ADDR_MATCH(bank, 32);
  724. DECLARE_ADDR_MATCH(page, 0x10000);
  725. DECLARE_ADDR_MATCH(col, 0x4000);
  726. static int write_and_test(struct pci_dev *dev, int where, u32 val)
  727. {
  728. u32 read;
  729. int count;
  730. debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
  731. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  732. where, val);
  733. for (count = 0; count < 10; count++) {
  734. if (count)
  735. msleep(100);
  736. pci_write_config_dword(dev, where, val);
  737. pci_read_config_dword(dev, where, &read);
  738. if (read == val)
  739. return 0;
  740. }
  741. i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
  742. "write=%08x. Read=%08x\n",
  743. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  744. where, val, read);
  745. return -EINVAL;
  746. }
  747. /*
  748. * This routine prepares the Memory Controller for error injection.
  749. * The error will be injected when some process tries to write to the
  750. * memory that matches the given criteria.
  751. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  752. * and col can be specified.
  753. * A -1 value for any of the mask items will make the MCU to ignore
  754. * that matching criteria for error injection.
  755. *
  756. * It should be noticed that the error will only happen after a write operation
  757. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  758. * inject mask, then it will produce just one error. Otherwise, it will repeat
  759. * until the injectmask would be cleaned.
  760. *
  761. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  762. * is reliable enough to check if the MC is using the
  763. * three channels. However, this is not clear at the datasheet.
  764. */
  765. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  766. const char *data, size_t count)
  767. {
  768. struct i7core_pvt *pvt = mci->pvt_info;
  769. u32 injectmask;
  770. u64 mask = 0;
  771. int rc;
  772. long enable;
  773. if (!pvt->pci_ch[pvt->inject.channel][0])
  774. return 0;
  775. rc = strict_strtoul(data, 10, &enable);
  776. if ((rc < 0))
  777. return 0;
  778. if (enable) {
  779. pvt->inject.enable = 1;
  780. } else {
  781. disable_inject(mci);
  782. return count;
  783. }
  784. /* Sets pvt->inject.dimm mask */
  785. if (pvt->inject.dimm < 0)
  786. mask |= 1LL << 41;
  787. else {
  788. if (pvt->channel[pvt->inject.channel].dimms > 2)
  789. mask |= (pvt->inject.dimm & 0x3LL) << 35;
  790. else
  791. mask |= (pvt->inject.dimm & 0x1LL) << 36;
  792. }
  793. /* Sets pvt->inject.rank mask */
  794. if (pvt->inject.rank < 0)
  795. mask |= 1LL << 40;
  796. else {
  797. if (pvt->channel[pvt->inject.channel].dimms > 2)
  798. mask |= (pvt->inject.rank & 0x1LL) << 34;
  799. else
  800. mask |= (pvt->inject.rank & 0x3LL) << 34;
  801. }
  802. /* Sets pvt->inject.bank mask */
  803. if (pvt->inject.bank < 0)
  804. mask |= 1LL << 39;
  805. else
  806. mask |= (pvt->inject.bank & 0x15LL) << 30;
  807. /* Sets pvt->inject.page mask */
  808. if (pvt->inject.page < 0)
  809. mask |= 1LL << 38;
  810. else
  811. mask |= (pvt->inject.page & 0xffff) << 14;
  812. /* Sets pvt->inject.column mask */
  813. if (pvt->inject.col < 0)
  814. mask |= 1LL << 37;
  815. else
  816. mask |= (pvt->inject.col & 0x3fff);
  817. /*
  818. * bit 0: REPEAT_EN
  819. * bits 1-2: MASK_HALF_CACHELINE
  820. * bit 3: INJECT_ECC
  821. * bit 4: INJECT_ADDR_PARITY
  822. */
  823. injectmask = (pvt->inject.type & 1) |
  824. (pvt->inject.section & 0x3) << 1 |
  825. (pvt->inject.type & 0x6) << (3 - 1);
  826. /* Unlock writes to registers - this register is write only */
  827. pci_write_config_dword(pvt->pci_noncore,
  828. MC_CFG_CONTROL, 0x2);
  829. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  830. MC_CHANNEL_ADDR_MATCH, mask);
  831. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  832. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  833. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  834. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  835. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  836. MC_CHANNEL_ERROR_INJECT, injectmask);
  837. /*
  838. * This is something undocumented, based on my tests
  839. * Without writing 8 to this register, errors aren't injected. Not sure
  840. * why.
  841. */
  842. pci_write_config_dword(pvt->pci_noncore,
  843. MC_CFG_CONTROL, 8);
  844. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  845. " inject 0x%08x\n",
  846. mask, pvt->inject.eccmask, injectmask);
  847. return count;
  848. }
  849. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  850. char *data)
  851. {
  852. struct i7core_pvt *pvt = mci->pvt_info;
  853. u32 injectmask;
  854. if (!pvt->pci_ch[pvt->inject.channel][0])
  855. return 0;
  856. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  857. MC_CHANNEL_ERROR_INJECT, &injectmask);
  858. debugf0("Inject error read: 0x%018x\n", injectmask);
  859. if (injectmask & 0x0c)
  860. pvt->inject.enable = 1;
  861. return sprintf(data, "%d\n", pvt->inject.enable);
  862. }
  863. #define DECLARE_COUNTER(param) \
  864. static ssize_t i7core_show_counter_##param( \
  865. struct mem_ctl_info *mci, \
  866. char *data) \
  867. { \
  868. struct i7core_pvt *pvt = mci->pvt_info; \
  869. \
  870. debugf1("%s() \n", __func__); \
  871. if (!pvt->ce_count_available || (pvt->is_registered)) \
  872. return sprintf(data, "data unavailable\n"); \
  873. return sprintf(data, "%lu\n", \
  874. pvt->udimm_ce_count[param]); \
  875. }
  876. #define ATTR_COUNTER(param) \
  877. { \
  878. .attr = { \
  879. .name = __stringify(udimm##param), \
  880. .mode = (S_IRUGO | S_IWUSR) \
  881. }, \
  882. .show = i7core_show_counter_##param \
  883. }
  884. DECLARE_COUNTER(0);
  885. DECLARE_COUNTER(1);
  886. DECLARE_COUNTER(2);
  887. /*
  888. * Sysfs struct
  889. */
  890. static struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
  891. ATTR_ADDR_MATCH(channel),
  892. ATTR_ADDR_MATCH(dimm),
  893. ATTR_ADDR_MATCH(rank),
  894. ATTR_ADDR_MATCH(bank),
  895. ATTR_ADDR_MATCH(page),
  896. ATTR_ADDR_MATCH(col),
  897. { .attr = { .name = NULL } }
  898. };
  899. static struct mcidev_sysfs_group i7core_inject_addrmatch = {
  900. .name = "inject_addrmatch",
  901. .mcidev_attr = i7core_addrmatch_attrs,
  902. };
  903. static struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
  904. ATTR_COUNTER(0),
  905. ATTR_COUNTER(1),
  906. ATTR_COUNTER(2),
  907. };
  908. static struct mcidev_sysfs_group i7core_udimm_counters = {
  909. .name = "all_channel_counts",
  910. .mcidev_attr = i7core_udimm_counters_attrs,
  911. };
  912. static struct mcidev_sysfs_attribute i7core_sysfs_attrs[] = {
  913. {
  914. .attr = {
  915. .name = "inject_section",
  916. .mode = (S_IRUGO | S_IWUSR)
  917. },
  918. .show = i7core_inject_section_show,
  919. .store = i7core_inject_section_store,
  920. }, {
  921. .attr = {
  922. .name = "inject_type",
  923. .mode = (S_IRUGO | S_IWUSR)
  924. },
  925. .show = i7core_inject_type_show,
  926. .store = i7core_inject_type_store,
  927. }, {
  928. .attr = {
  929. .name = "inject_eccmask",
  930. .mode = (S_IRUGO | S_IWUSR)
  931. },
  932. .show = i7core_inject_eccmask_show,
  933. .store = i7core_inject_eccmask_store,
  934. }, {
  935. .grp = &i7core_inject_addrmatch,
  936. }, {
  937. .attr = {
  938. .name = "inject_enable",
  939. .mode = (S_IRUGO | S_IWUSR)
  940. },
  941. .show = i7core_inject_enable_show,
  942. .store = i7core_inject_enable_store,
  943. },
  944. { .attr = { .name = NULL } }, /* Reserved for udimm counters */
  945. { .attr = { .name = NULL } }
  946. };
  947. /****************************************************************************
  948. Device initialization routines: put/get, init/exit
  949. ****************************************************************************/
  950. /*
  951. * i7core_put_devices 'put' all the devices that we have
  952. * reserved via 'get'
  953. */
  954. static void i7core_put_devices(struct i7core_dev *i7core_dev)
  955. {
  956. int i;
  957. debugf0(__FILE__ ": %s()\n", __func__);
  958. for (i = 0; i < i7core_dev->n_devs; i++) {
  959. struct pci_dev *pdev = i7core_dev->pdev[i];
  960. if (!pdev)
  961. continue;
  962. debugf0("Removing dev %02x:%02x.%d\n",
  963. pdev->bus->number,
  964. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  965. pci_dev_put(pdev);
  966. }
  967. kfree(i7core_dev->pdev);
  968. list_del(&i7core_dev->list);
  969. kfree(i7core_dev);
  970. }
  971. static void i7core_put_all_devices(void)
  972. {
  973. struct i7core_dev *i7core_dev, *tmp;
  974. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list)
  975. i7core_put_devices(i7core_dev);
  976. }
  977. static void __init i7core_xeon_pci_fixup(int dev_id)
  978. {
  979. struct pci_dev *pdev = NULL;
  980. int i;
  981. /*
  982. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core pci buses
  983. * aren't announced by acpi. So, we need to use a legacy scan probing
  984. * to detect them
  985. */
  986. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
  987. if (unlikely(!pdev)) {
  988. for (i = 0; i < MAX_SOCKET_BUSES; i++)
  989. pcibios_scan_specific_bus(255-i);
  990. }
  991. }
  992. /*
  993. * i7core_get_devices Find and perform 'get' operation on the MCH's
  994. * device/functions we want to reference for this driver
  995. *
  996. * Need to 'get' device 16 func 1 and func 2
  997. */
  998. int i7core_get_onedevice(struct pci_dev **prev, int devno,
  999. struct pci_id_descr *dev_descr, unsigned n_devs)
  1000. {
  1001. struct i7core_dev *i7core_dev;
  1002. struct pci_dev *pdev = NULL;
  1003. u8 bus = 0;
  1004. u8 socket = 0;
  1005. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1006. dev_descr->dev_id, *prev);
  1007. /*
  1008. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
  1009. * is at addr 8086:2c40, instead of 8086:2c41. So, we need
  1010. * to probe for the alternate address in case of failure
  1011. */
  1012. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev)
  1013. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1014. PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
  1015. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE && !pdev) {
  1016. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1017. PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
  1018. *prev);
  1019. if (!pdev)
  1020. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1021. PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2,
  1022. *prev);
  1023. }
  1024. if (!pdev) {
  1025. if (*prev) {
  1026. *prev = pdev;
  1027. return 0;
  1028. }
  1029. if (dev_descr->optional)
  1030. return 0;
  1031. i7core_printk(KERN_ERR,
  1032. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1033. dev_descr->dev, dev_descr->func,
  1034. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1035. /* End of list, leave */
  1036. return -ENODEV;
  1037. }
  1038. bus = pdev->bus->number;
  1039. if (bus == 0x3f)
  1040. socket = 0;
  1041. else
  1042. socket = 255 - bus;
  1043. i7core_dev = get_i7core_dev(socket);
  1044. if (!i7core_dev) {
  1045. i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
  1046. if (!i7core_dev)
  1047. return -ENOMEM;
  1048. i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * n_devs,
  1049. GFP_KERNEL);
  1050. if (!i7core_dev->pdev) {
  1051. kfree(i7core_dev);
  1052. return -ENOMEM;
  1053. }
  1054. i7core_dev->socket = socket;
  1055. i7core_dev->n_devs = n_devs;
  1056. list_add_tail(&i7core_dev->list, &i7core_edac_list);
  1057. }
  1058. if (i7core_dev->pdev[devno]) {
  1059. i7core_printk(KERN_ERR,
  1060. "Duplicated device for "
  1061. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1062. bus, dev_descr->dev, dev_descr->func,
  1063. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1064. pci_dev_put(pdev);
  1065. return -ENODEV;
  1066. }
  1067. i7core_dev->pdev[devno] = pdev;
  1068. /* Sanity check */
  1069. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1070. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1071. i7core_printk(KERN_ERR,
  1072. "Device PCI ID %04x:%04x "
  1073. "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
  1074. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1075. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1076. bus, dev_descr->dev, dev_descr->func);
  1077. return -ENODEV;
  1078. }
  1079. /* Be sure that the device is enabled */
  1080. if (unlikely(pci_enable_device(pdev) < 0)) {
  1081. i7core_printk(KERN_ERR,
  1082. "Couldn't enable "
  1083. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1084. bus, dev_descr->dev, dev_descr->func,
  1085. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1086. return -ENODEV;
  1087. }
  1088. debugf0("Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1089. socket, bus, dev_descr->dev,
  1090. dev_descr->func,
  1091. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1092. *prev = pdev;
  1093. return 0;
  1094. }
  1095. static int i7core_get_devices(struct pci_id_descr dev_descr[], unsigned n_devs)
  1096. {
  1097. int i, rc;
  1098. struct pci_dev *pdev = NULL;
  1099. for (i = 0; i < n_devs; i++) {
  1100. pdev = NULL;
  1101. do {
  1102. rc = i7core_get_onedevice(&pdev, i, &dev_descr[i],
  1103. n_devs);
  1104. if (rc < 0) {
  1105. i7core_put_all_devices();
  1106. return -ENODEV;
  1107. }
  1108. } while (pdev);
  1109. }
  1110. return 0;
  1111. }
  1112. static int mci_bind_devs(struct mem_ctl_info *mci,
  1113. struct i7core_dev *i7core_dev)
  1114. {
  1115. struct i7core_pvt *pvt = mci->pvt_info;
  1116. struct pci_dev *pdev;
  1117. int i, func, slot;
  1118. /* Associates i7core_dev and mci for future usage */
  1119. pvt->i7core_dev = i7core_dev;
  1120. i7core_dev->mci = mci;
  1121. pvt->is_registered = 0;
  1122. for (i = 0; i < i7core_dev->n_devs; i++) {
  1123. pdev = i7core_dev->pdev[i];
  1124. if (!pdev)
  1125. continue;
  1126. func = PCI_FUNC(pdev->devfn);
  1127. slot = PCI_SLOT(pdev->devfn);
  1128. if (slot == 3) {
  1129. if (unlikely(func > MAX_MCR_FUNC))
  1130. goto error;
  1131. pvt->pci_mcr[func] = pdev;
  1132. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1133. if (unlikely(func > MAX_CHAN_FUNC))
  1134. goto error;
  1135. pvt->pci_ch[slot - 4][func] = pdev;
  1136. } else if (!slot && !func)
  1137. pvt->pci_noncore = pdev;
  1138. else
  1139. goto error;
  1140. debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
  1141. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1142. pdev, i7core_dev->socket);
  1143. if (PCI_SLOT(pdev->devfn) == 3 &&
  1144. PCI_FUNC(pdev->devfn) == 2)
  1145. pvt->is_registered = 1;
  1146. }
  1147. /*
  1148. * Add extra nodes to count errors on udimm
  1149. * For registered memory, this is not needed, since the counters
  1150. * are already displayed at the standard locations
  1151. */
  1152. if (!pvt->is_registered)
  1153. i7core_sysfs_attrs[ARRAY_SIZE(i7core_sysfs_attrs)-2].grp =
  1154. &i7core_udimm_counters;
  1155. return 0;
  1156. error:
  1157. i7core_printk(KERN_ERR, "Device %d, function %d "
  1158. "is out of the expected range\n",
  1159. slot, func);
  1160. return -EINVAL;
  1161. }
  1162. /****************************************************************************
  1163. Error check routines
  1164. ****************************************************************************/
  1165. static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
  1166. int chan, int dimm, int add)
  1167. {
  1168. char *msg;
  1169. struct i7core_pvt *pvt = mci->pvt_info;
  1170. int row = pvt->csrow_map[chan][dimm], i;
  1171. for (i = 0; i < add; i++) {
  1172. msg = kasprintf(GFP_KERNEL, "Corrected error "
  1173. "(Socket=%d channel=%d dimm=%d)",
  1174. pvt->i7core_dev->socket, chan, dimm);
  1175. edac_mc_handle_fbd_ce(mci, row, 0, msg);
  1176. kfree (msg);
  1177. }
  1178. }
  1179. static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
  1180. int chan, int new0, int new1, int new2)
  1181. {
  1182. struct i7core_pvt *pvt = mci->pvt_info;
  1183. int add0 = 0, add1 = 0, add2 = 0;
  1184. /* Updates CE counters if it is not the first time here */
  1185. if (pvt->ce_count_available) {
  1186. /* Updates CE counters */
  1187. add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
  1188. add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
  1189. add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
  1190. if (add2 < 0)
  1191. add2 += 0x7fff;
  1192. pvt->rdimm_ce_count[chan][2] += add2;
  1193. if (add1 < 0)
  1194. add1 += 0x7fff;
  1195. pvt->rdimm_ce_count[chan][1] += add1;
  1196. if (add0 < 0)
  1197. add0 += 0x7fff;
  1198. pvt->rdimm_ce_count[chan][0] += add0;
  1199. } else
  1200. pvt->ce_count_available = 1;
  1201. /* Store the new values */
  1202. pvt->rdimm_last_ce_count[chan][2] = new2;
  1203. pvt->rdimm_last_ce_count[chan][1] = new1;
  1204. pvt->rdimm_last_ce_count[chan][0] = new0;
  1205. /*updated the edac core */
  1206. if (add0 != 0)
  1207. i7core_rdimm_update_csrow(mci, chan, 0, add0);
  1208. if (add1 != 0)
  1209. i7core_rdimm_update_csrow(mci, chan, 1, add1);
  1210. if (add2 != 0)
  1211. i7core_rdimm_update_csrow(mci, chan, 2, add2);
  1212. }
  1213. static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1214. {
  1215. struct i7core_pvt *pvt = mci->pvt_info;
  1216. u32 rcv[3][2];
  1217. int i, new0, new1, new2;
  1218. /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
  1219. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
  1220. &rcv[0][0]);
  1221. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
  1222. &rcv[0][1]);
  1223. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
  1224. &rcv[1][0]);
  1225. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
  1226. &rcv[1][1]);
  1227. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
  1228. &rcv[2][0]);
  1229. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
  1230. &rcv[2][1]);
  1231. for (i = 0 ; i < 3; i++) {
  1232. debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
  1233. (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
  1234. /*if the channel has 3 dimms*/
  1235. if (pvt->channel[i].dimms > 2) {
  1236. new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
  1237. new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
  1238. new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
  1239. } else {
  1240. new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
  1241. DIMM_BOT_COR_ERR(rcv[i][0]);
  1242. new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
  1243. DIMM_BOT_COR_ERR(rcv[i][1]);
  1244. new2 = 0;
  1245. }
  1246. i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
  1247. }
  1248. }
  1249. /* This function is based on the device 3 function 4 registers as described on:
  1250. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1251. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1252. * also available at:
  1253. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1254. */
  1255. static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1256. {
  1257. struct i7core_pvt *pvt = mci->pvt_info;
  1258. u32 rcv1, rcv0;
  1259. int new0, new1, new2;
  1260. if (!pvt->pci_mcr[4]) {
  1261. debugf0("%s MCR registers not found\n", __func__);
  1262. return;
  1263. }
  1264. /* Corrected test errors */
  1265. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1266. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1267. /* Store the new values */
  1268. new2 = DIMM2_COR_ERR(rcv1);
  1269. new1 = DIMM1_COR_ERR(rcv0);
  1270. new0 = DIMM0_COR_ERR(rcv0);
  1271. /* Updates CE counters if it is not the first time here */
  1272. if (pvt->ce_count_available) {
  1273. /* Updates CE counters */
  1274. int add0, add1, add2;
  1275. add2 = new2 - pvt->udimm_last_ce_count[2];
  1276. add1 = new1 - pvt->udimm_last_ce_count[1];
  1277. add0 = new0 - pvt->udimm_last_ce_count[0];
  1278. if (add2 < 0)
  1279. add2 += 0x7fff;
  1280. pvt->udimm_ce_count[2] += add2;
  1281. if (add1 < 0)
  1282. add1 += 0x7fff;
  1283. pvt->udimm_ce_count[1] += add1;
  1284. if (add0 < 0)
  1285. add0 += 0x7fff;
  1286. pvt->udimm_ce_count[0] += add0;
  1287. if (add0 | add1 | add2)
  1288. i7core_printk(KERN_ERR, "New Corrected error(s): "
  1289. "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
  1290. add0, add1, add2);
  1291. } else
  1292. pvt->ce_count_available = 1;
  1293. /* Store the new values */
  1294. pvt->udimm_last_ce_count[2] = new2;
  1295. pvt->udimm_last_ce_count[1] = new1;
  1296. pvt->udimm_last_ce_count[0] = new0;
  1297. }
  1298. /*
  1299. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1300. * Architectures Software Developer’s Manual Volume 3B.
  1301. * Nehalem are defined as family 0x06, model 0x1a
  1302. *
  1303. * The MCA registers used here are the following ones:
  1304. * struct mce field MCA Register
  1305. * m->status MSR_IA32_MC8_STATUS
  1306. * m->addr MSR_IA32_MC8_ADDR
  1307. * m->misc MSR_IA32_MC8_MISC
  1308. * In the case of Nehalem, the error information is masked at .status and .misc
  1309. * fields
  1310. */
  1311. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1312. struct mce *m)
  1313. {
  1314. struct i7core_pvt *pvt = mci->pvt_info;
  1315. char *type, *optype, *err, *msg;
  1316. unsigned long error = m->status & 0x1ff0000l;
  1317. u32 optypenum = (m->status >> 4) & 0x07;
  1318. u32 core_err_cnt = (m->status >> 38) && 0x7fff;
  1319. u32 dimm = (m->misc >> 16) & 0x3;
  1320. u32 channel = (m->misc >> 18) & 0x3;
  1321. u32 syndrome = m->misc >> 32;
  1322. u32 errnum = find_first_bit(&error, 32);
  1323. int csrow;
  1324. if (m->mcgstatus & 1)
  1325. type = "FATAL";
  1326. else
  1327. type = "NON_FATAL";
  1328. switch (optypenum) {
  1329. case 0:
  1330. optype = "generic undef request";
  1331. break;
  1332. case 1:
  1333. optype = "read error";
  1334. break;
  1335. case 2:
  1336. optype = "write error";
  1337. break;
  1338. case 3:
  1339. optype = "addr/cmd error";
  1340. break;
  1341. case 4:
  1342. optype = "scrubbing error";
  1343. break;
  1344. default:
  1345. optype = "reserved";
  1346. break;
  1347. }
  1348. switch (errnum) {
  1349. case 16:
  1350. err = "read ECC error";
  1351. break;
  1352. case 17:
  1353. err = "RAS ECC error";
  1354. break;
  1355. case 18:
  1356. err = "write parity error";
  1357. break;
  1358. case 19:
  1359. err = "redundacy loss";
  1360. break;
  1361. case 20:
  1362. err = "reserved";
  1363. break;
  1364. case 21:
  1365. err = "memory range error";
  1366. break;
  1367. case 22:
  1368. err = "RTID out of range";
  1369. break;
  1370. case 23:
  1371. err = "address parity error";
  1372. break;
  1373. case 24:
  1374. err = "byte enable parity error";
  1375. break;
  1376. default:
  1377. err = "unknown";
  1378. }
  1379. /* FIXME: should convert addr into bank and rank information */
  1380. msg = kasprintf(GFP_ATOMIC,
  1381. "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
  1382. "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
  1383. type, (long long) m->addr, m->cpu, dimm, channel,
  1384. syndrome, core_err_cnt, (long long)m->status,
  1385. (long long)m->misc, optype, err);
  1386. debugf0("%s", msg);
  1387. csrow = pvt->csrow_map[channel][dimm];
  1388. /* Call the helper to output message */
  1389. if (m->mcgstatus & 1)
  1390. edac_mc_handle_fbd_ue(mci, csrow, 0,
  1391. 0 /* FIXME: should be channel here */, msg);
  1392. else if (!pvt->is_registered)
  1393. edac_mc_handle_fbd_ce(mci, csrow,
  1394. 0 /* FIXME: should be channel here */, msg);
  1395. kfree(msg);
  1396. }
  1397. /*
  1398. * i7core_check_error Retrieve and process errors reported by the
  1399. * hardware. Called by the Core module.
  1400. */
  1401. static void i7core_check_error(struct mem_ctl_info *mci)
  1402. {
  1403. struct i7core_pvt *pvt = mci->pvt_info;
  1404. int i;
  1405. unsigned count = 0;
  1406. struct mce *m;
  1407. /*
  1408. * MCE first step: Copy all mce errors into a temporary buffer
  1409. * We use a double buffering here, to reduce the risk of
  1410. * loosing an error.
  1411. */
  1412. smp_rmb();
  1413. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1414. % MCE_LOG_LEN;
  1415. if (!count)
  1416. goto check_ce_error;
  1417. m = pvt->mce_outentry;
  1418. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1419. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1420. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1421. smp_wmb();
  1422. pvt->mce_in = 0;
  1423. count -= l;
  1424. m += l;
  1425. }
  1426. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1427. smp_wmb();
  1428. pvt->mce_in += count;
  1429. smp_rmb();
  1430. if (pvt->mce_overrun) {
  1431. i7core_printk(KERN_ERR, "Lost %d memory errors\n",
  1432. pvt->mce_overrun);
  1433. smp_wmb();
  1434. pvt->mce_overrun = 0;
  1435. }
  1436. /*
  1437. * MCE second step: parse errors and display
  1438. */
  1439. for (i = 0; i < count; i++)
  1440. i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
  1441. /*
  1442. * Now, let's increment CE error counts
  1443. */
  1444. check_ce_error:
  1445. if (!pvt->is_registered)
  1446. i7core_udimm_check_mc_ecc_err(mci);
  1447. else
  1448. i7core_rdimm_check_mc_ecc_err(mci);
  1449. }
  1450. /*
  1451. * i7core_mce_check_error Replicates mcelog routine to get errors
  1452. * This routine simply queues mcelog errors, and
  1453. * return. The error itself should be handled later
  1454. * by i7core_check_error.
  1455. * WARNING: As this routine should be called at NMI time, extra care should
  1456. * be taken to avoid deadlocks, and to be as fast as possible.
  1457. */
  1458. static int i7core_mce_check_error(void *priv, struct mce *mce)
  1459. {
  1460. struct mem_ctl_info *mci = priv;
  1461. struct i7core_pvt *pvt = mci->pvt_info;
  1462. /*
  1463. * Just let mcelog handle it if the error is
  1464. * outside the memory controller
  1465. */
  1466. if (((mce->status & 0xffff) >> 7) != 1)
  1467. return 0;
  1468. /* Bank 8 registers are the only ones that we know how to handle */
  1469. if (mce->bank != 8)
  1470. return 0;
  1471. #ifdef CONFIG_SMP
  1472. /* Only handle if it is the right mc controller */
  1473. if (cpu_data(mce->cpu).phys_proc_id != pvt->i7core_dev->socket)
  1474. return 0;
  1475. #endif
  1476. smp_rmb();
  1477. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1478. smp_wmb();
  1479. pvt->mce_overrun++;
  1480. return 0;
  1481. }
  1482. /* Copy memory error at the ringbuffer */
  1483. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1484. smp_wmb();
  1485. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1486. /* Handle fatal errors immediately */
  1487. if (mce->mcgstatus & 1)
  1488. i7core_check_error(mci);
  1489. /* Advice mcelog that the error were handled */
  1490. return 1;
  1491. }
  1492. static int i7core_register_mci(struct i7core_dev *i7core_dev,
  1493. int num_channels, int num_csrows)
  1494. {
  1495. struct mem_ctl_info *mci;
  1496. struct i7core_pvt *pvt;
  1497. int csrow = 0;
  1498. int rc;
  1499. /* allocate a new MC control structure */
  1500. mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels,
  1501. i7core_dev->socket);
  1502. if (unlikely(!mci))
  1503. return -ENOMEM;
  1504. debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
  1505. /* record ptr to the generic device */
  1506. mci->dev = &i7core_dev->pdev[0]->dev;
  1507. pvt = mci->pvt_info;
  1508. memset(pvt, 0, sizeof(*pvt));
  1509. /*
  1510. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1511. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1512. * memory channels
  1513. */
  1514. mci->mtype_cap = MEM_FLAG_DDR3;
  1515. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1516. mci->edac_cap = EDAC_FLAG_NONE;
  1517. mci->mod_name = "i7core_edac.c";
  1518. mci->mod_ver = I7CORE_REVISION;
  1519. mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
  1520. i7core_dev->socket);
  1521. mci->dev_name = pci_name(i7core_dev->pdev[0]);
  1522. mci->ctl_page_to_phys = NULL;
  1523. mci->mc_driver_sysfs_attributes = i7core_sysfs_attrs;
  1524. /* Set the function pointer to an actual operation function */
  1525. mci->edac_check = i7core_check_error;
  1526. /* Store pci devices at mci for faster access */
  1527. rc = mci_bind_devs(mci, i7core_dev);
  1528. if (unlikely(rc < 0))
  1529. goto fail;
  1530. /* Get dimm basic config */
  1531. get_dimm_config(mci, &csrow);
  1532. /* add this new MC control structure to EDAC's list of MCs */
  1533. if (unlikely(edac_mc_add_mc(mci))) {
  1534. debugf0("MC: " __FILE__
  1535. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1536. /* FIXME: perhaps some code should go here that disables error
  1537. * reporting if we just enabled it
  1538. */
  1539. rc = -EINVAL;
  1540. goto fail;
  1541. }
  1542. /* allocating generic PCI control info */
  1543. i7core_pci = edac_pci_create_generic_ctl(&i7core_dev->pdev[0]->dev,
  1544. EDAC_MOD_STR);
  1545. if (unlikely(!i7core_pci)) {
  1546. printk(KERN_WARNING
  1547. "%s(): Unable to create PCI control\n",
  1548. __func__);
  1549. printk(KERN_WARNING
  1550. "%s(): PCI error report via EDAC not setup\n",
  1551. __func__);
  1552. }
  1553. /* Default error mask is any memory */
  1554. pvt->inject.channel = 0;
  1555. pvt->inject.dimm = -1;
  1556. pvt->inject.rank = -1;
  1557. pvt->inject.bank = -1;
  1558. pvt->inject.page = -1;
  1559. pvt->inject.col = -1;
  1560. /* Registers on edac_mce in order to receive memory errors */
  1561. pvt->edac_mce.priv = mci;
  1562. pvt->edac_mce.check_error = i7core_mce_check_error;
  1563. rc = edac_mce_register(&pvt->edac_mce);
  1564. if (unlikely(rc < 0)) {
  1565. debugf0("MC: " __FILE__
  1566. ": %s(): failed edac_mce_register()\n", __func__);
  1567. }
  1568. fail:
  1569. edac_mc_free(mci);
  1570. return rc;
  1571. }
  1572. /*
  1573. * i7core_probe Probe for ONE instance of device to see if it is
  1574. * present.
  1575. * return:
  1576. * 0 for FOUND a device
  1577. * < 0 for error code
  1578. */
  1579. static int __devinit i7core_probe(struct pci_dev *pdev,
  1580. const struct pci_device_id *id)
  1581. {
  1582. int dev_idx = id->driver_data;
  1583. int rc;
  1584. struct i7core_dev *i7core_dev;
  1585. /*
  1586. * All memory controllers are allocated at the first pass.
  1587. */
  1588. if (unlikely(dev_idx >= 1))
  1589. return -EINVAL;
  1590. /* get the pci devices we want to reserve for our use */
  1591. mutex_lock(&i7core_edac_lock);
  1592. if (pdev->device == PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0) {
  1593. printk(KERN_INFO "i7core_edac: detected a "
  1594. "Lynnfield processor\n");
  1595. rc = i7core_get_devices(pci_dev_descr_lynnfield,
  1596. ARRAY_SIZE(pci_dev_descr_lynnfield));
  1597. } else {
  1598. printk(KERN_INFO "i7core_edac: detected a "
  1599. "Nehalem/Nehalem-EP processor\n");
  1600. rc = i7core_get_devices(pci_dev_descr_i7core,
  1601. ARRAY_SIZE(pci_dev_descr_i7core));
  1602. }
  1603. if (unlikely(rc < 0))
  1604. goto fail0;
  1605. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  1606. int channels;
  1607. int csrows;
  1608. /* Check the number of active and not disabled channels */
  1609. rc = i7core_get_active_channels(i7core_dev->socket,
  1610. &channels, &csrows);
  1611. if (unlikely(rc < 0))
  1612. goto fail1;
  1613. rc = i7core_register_mci(i7core_dev, channels, csrows);
  1614. if (unlikely(rc < 0))
  1615. goto fail1;
  1616. }
  1617. i7core_printk(KERN_INFO, "Driver loaded.\n");
  1618. mutex_unlock(&i7core_edac_lock);
  1619. return 0;
  1620. fail1:
  1621. i7core_put_all_devices();
  1622. fail0:
  1623. mutex_unlock(&i7core_edac_lock);
  1624. return rc;
  1625. }
  1626. /*
  1627. * i7core_remove destructor for one instance of device
  1628. *
  1629. */
  1630. static void __devexit i7core_remove(struct pci_dev *pdev)
  1631. {
  1632. struct mem_ctl_info *mci;
  1633. struct i7core_dev *i7core_dev, *tmp;
  1634. debugf0(__FILE__ ": %s()\n", __func__);
  1635. if (i7core_pci)
  1636. edac_pci_release_generic_ctl(i7core_pci);
  1637. /*
  1638. * we have a trouble here: pdev value for removal will be wrong, since
  1639. * it will point to the X58 register used to detect that the machine
  1640. * is a Nehalem or upper design. However, due to the way several PCI
  1641. * devices are grouped together to provide MC functionality, we need
  1642. * to use a different method for releasing the devices
  1643. */
  1644. mutex_lock(&i7core_edac_lock);
  1645. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
  1646. mci = edac_mc_del_mc(&i7core_dev->pdev[0]->dev);
  1647. if (mci) {
  1648. struct i7core_pvt *pvt = mci->pvt_info;
  1649. i7core_dev = pvt->i7core_dev;
  1650. edac_mce_unregister(&pvt->edac_mce);
  1651. kfree(mci->ctl_name);
  1652. edac_mc_free(mci);
  1653. i7core_put_devices(i7core_dev);
  1654. } else {
  1655. i7core_printk(KERN_ERR,
  1656. "Couldn't find mci for socket %d\n",
  1657. i7core_dev->socket);
  1658. }
  1659. }
  1660. mutex_unlock(&i7core_edac_lock);
  1661. }
  1662. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  1663. /*
  1664. * i7core_driver pci_driver structure for this module
  1665. *
  1666. */
  1667. static struct pci_driver i7core_driver = {
  1668. .name = "i7core_edac",
  1669. .probe = i7core_probe,
  1670. .remove = __devexit_p(i7core_remove),
  1671. .id_table = i7core_pci_tbl,
  1672. };
  1673. /*
  1674. * i7core_init Module entry function
  1675. * Try to initialize this module for its devices
  1676. */
  1677. static int __init i7core_init(void)
  1678. {
  1679. int pci_rc;
  1680. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1681. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1682. opstate_init();
  1683. i7core_xeon_pci_fixup(pci_dev_descr_i7core[0].dev_id);
  1684. pci_rc = pci_register_driver(&i7core_driver);
  1685. if (pci_rc >= 0)
  1686. return 0;
  1687. i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1688. pci_rc);
  1689. return pci_rc;
  1690. }
  1691. /*
  1692. * i7core_exit() Module exit function
  1693. * Unregister the driver
  1694. */
  1695. static void __exit i7core_exit(void)
  1696. {
  1697. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1698. pci_unregister_driver(&i7core_driver);
  1699. }
  1700. module_init(i7core_init);
  1701. module_exit(i7core_exit);
  1702. MODULE_LICENSE("GPL");
  1703. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1704. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1705. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  1706. I7CORE_REVISION);
  1707. module_param(edac_op_state, int, 0444);
  1708. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");