i7core_edac.c 54 KB

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